Socket Connections

Initially, a socket is created in the unconnected state, meaning the socket is not associated with any foreign destination. The connect() call binds a permanent destination to a socket, placing it in the connected state. An application program must call connect() to establish a connection before it can transfer data through a reliable stream socket. Sockets used with connectionless datagram services need not be connected before they are used, but connecting sockets makes it possible to transfer data without specifying the destination each time.

The semantics of the connect() call depend on the underlying protocols. In the case of TCP, the connect() call builds a TCP connection with the destination, or returns an error if it cannot. In the case of connectionless services, the connect() call does nothing more than store the destination address locally.

Connections are established between a client process and a server process. In a connection-oriented network environment, a client process initiates a connection and a server process receives, or responds to, a connection. The client and server interactions occur as follows:

An example of a client application using the connect() call to request a connection is: AnApplicationUsingtheconnect ( )Call

int s;
struct sockaddr_in servername;
int rc;
...
memset(&servername, 0, sizeof(servname));
servname.sin_len = sizeof(servname);
servername.sin_family = AF_INET;
servername.sin_addr.s_addr = inet_addr("129.5.24.1");
servername.sin_port = htons(1024);
...
rc = connect(s, (struct sockaddr *) &servername, sizeof(servername));

The connect() call attempts to connect socket s to the server with name supplied in servername. This could be the server that was used in the previous bind() example. With TCP stream sockets the caller blocks until the connection is accepted by the server. On successful return from connect(), the socket s is associated with the connection to the server. See ioctl() for additional information about determining blocking and nonblocking behavior. Note that the sockaddr_in structure should be cleared before calling connect(). For a more detailed description, see connect().

The following figure shows an example of using the gethostbyname() network utility routine to find out the internet address of serverhost from the name server or the ETC\HOSTS file:
An Application Using the gethostbyname() Call

int s;
struct sockaddr_in servername;
char *hostname = "serverhost";
int rc;
int connect(int s, struct sockaddr *name, int namelen);  /* extracted from sys\socket.h */
struct hostent *hp;
...

hp = gethostbyname(hostname);

/* clear the structure */
memset(&servername, 0, sizeof(servname));
servername.sin_len = sizeof(servername);
servername.sin_family = AF_INET;
servername.sin_addr.s_addr = *((u_long *)hp->h_addr);
servername.sin_port = htons(1024);
...
rc = connect(s,(struct sockaddr *)&servername,sizeof(servername));


[Back: Binding Names to Sockets]
[Next: Obtaining Socket Addresses]