connect Function

 < Day Day Up > 



connect Function

The connect function is a client-specific function that connects a client socket to a server socket. With a previously created socket, the connect function is called with the address structure identifying the host and port to which to connect. The connect function prototype appears as:

#include <sys/types.h> #include <sys/socket.h> int connect( int sock,                  struct sockaddr *name, socklen_t namelen );

The purpose of the connect function differs based upon the type of socket being used. With a SOCK_STREAM socket (TCP), the connect function attempts to create a stream connection to the defined peer in the name address. With a SOCK_DGRAM socket (UDP), the connect function simply associates the local socket with a peer datagram socket. Recall that UDP is unconnected, so the primary function is to permit datagram sockets to call the send function instead of sendto (which requires the peer address information in addition to the message to send). Because SOCK_STREAM sockets are connection-oriented, a connect may be performed once before it is closed. Because SOCK_DGRAM sockets are unconnected, the datagram socket may call connect multiple times to associate itself with a new peer datagram socket.

Listing 3.5 provides an example of the connect function usage for a stream socket. After the stream socket is created, the peer address to which we’ll connect is initialized (servaddr). Using the connect function, the stream socket (connectionFd) is connected to the peer. The connect function returns zero on success, representing a successful connect to the peer. Otherwise, a –1 is returned representing an error. On Unix systems, the errno variable can be checked for the error. Other TCP/IP stacks may have other variables or functions for checking error status.

Listing 3.5 Sample code for the connect function in a stream socket setting.

start example
int connectionFd, in; struct sockaddr_in servaddr; char timebuffer[MAX_BUFFER+1]; connectionFd = socket( AF_INET, SOCK_STREAM, 0 ); memset( &servaddr, 0, sizeof(servaddr) ); servaddr.sin_family = AF_INET; servaddr.sin_port = htons( 25 ); servaddr.sin_addr.s_addr = inet_addr( "10.0.0.200" ); connect( connectionFd, (struct sockaddr_in *)&servaddr,             sizeof(servaddr) );
end example

In Figure 3.5, we see a progression of connection state through a call with the connect function. In step (a), a socket is created at the client (left-hand side). The server in this step has already created a socket (shown initialized with port 25), and is available for connections via the accept function (right-hand side). At this point, no connectivity exists between the client and server applications.

click to expand
Figure 3.5: Graphical progression of the connect function.

Continuing at step (b), the connect function has been performed and the three-way handshake begins. Once the three-way handshake is complete, we continue to step (c) in which the connect function returns at the client and the accept function returns at the server. These two socket endpoints are now connected (otherwise known as an established connection) in step (c) and data communication may occur.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net