@CHIP-RTOS C Library V2.00 - TCP/IP API
tcp_connect
Open a client TCP/IP socket connection to a specified
host. int tcp_connect ( const char far *DestIPStr,
unsigned int ClientPort, unsigned int HostPort,
int *error ); Parameters
DestIPStr
- Pointer to server's IP address, expressed
as a null terminated ASCII string in network dot notation.
ClientPort
- Optional local port number to assign to socket.
Set this to zero if a bind operation is not desired (random port).
HostPort
- Remote port number to which connection is desired.
error
- Output parameter: Failure
code, 0 on success.
Return Value
- -1 = Failure (see error
output parameter)
else new socket descriptor
Comments
- This function calls the required set of TCP/IP functions to establish
a client connection to a host.
Pseudo code:
struct sockaddr_in addr;
int sd = opensocket(SOCK_STREAM, error) ;
addr.sin_family = PF_INET ;
addr.sin_addr.s_addr = 0 ;
IF ClientPort != 0 THEN
addr.sin_port = htons(ClientPort) ;
bind(sd, &addr, error) ;
ENDIF
addr.sin_port = htons(HostPort) ;
error = inet_addr(DestIPStr, &addr.sin_addr.s_addr) ;
error = connect (sd, &addr) ;
return sd ;
The error handling is not shown in the above simplified pseudo code.
In case of errors, the socket is closed by this API function and a return
is made from the point of the error with return value -1.
On success, the new socket descriptor is returned. The caller is then
responsible for calling closesocket when
finished with the connection.
See Also
This API List
List of C Libraries
@CHIP-RTOS Main Index
End of document
|