Sample Client

 < Day Day Up > 



Now, let’s look at the client for the previously discussed Daytime protocol server. The C language client is shown in Listing 2.2. For brevity, we omit the include files in this example.

Listing 2.2 Daytime protocol client (client.c).

start example
 1   int main ( )  2   {  3     int connectionFd, in;  4     struct sockaddr_in servaddr;  5     char timebuffer[MAX_BUFFER+1];  6  7     connectionFd = socket(AF_INET, SOCK_STREAM, 0);  8  9     memset(&servaddr, 0, sizeof(servaddr)); 10     servaddr.sin_family = AF_INET; 11     servaddr.sin_port = htons(DAYTIME_SERVER_PORT); 12 13     servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");  14 15     connect(connectionFd,  16             (struct sockaddr_in *)&servaddr,  17             sizeof(servaddr)); 18 19     while ( (in = read(connectionFd, timebuffer,  20              MAX_BUFFER)) > 0) { 21 22       timebuffer[in] = 0; 23       printf("\n%s", timebuffer); 24 25     } 26 27     close(connectionFd); 28 29     return(0); 30   }
end example

Source Discussion

Our main program begins on line 1, with a set of local variables declared in lines 3–5. Line 7 begins our function with the creation of a new socket (as with the server, a stream socket). We clear out our address structure on line 9 and then initialize a few of the entries (family and port). Note that in this case, we’re defining an address structure for the endpoint to which we want to connect. Recall from the server example, that this same structure was used to bind the name to the socket. This symmetry is important to note as it will be used to link the two endpoints together.

In line 13, we define the last element of the address structure, the address of the host to which we’re going to connect. We’re using the loopback address here (127.0.0.1), which assumes that the client and server are running on the same host. This line could easily reference a different IP address (the address on which the server is actually running) to support connecting over the Internet. The address is converted from a string to a 32-bit value using the inet_addr function. This function maps the string “127.0.0.1” to the numeric 0x7f000001, which is the proper mapping for the sockaddr_in structure.

Tip 

The loopback address is a special address that simply represents the current host. The IP address for the loopback interface is 127.0.0.1.

We create the connection to the server from the client using the connect function (line 13), specifying connectionFd as our socket endpoint and servaddr as the endpoint to which we want to connect. The connect function is specific to stream sockets and performs the connection synchronization and negotiation with the server using TCP. When the connect call returns, either an error has occurred, or we’re connected to the server. For brevity, we’ll assume a successful connection, but a check of the return status can easily determine success or failure of this call.

At line 19, we attempt to read from the socket using the read call. We specify three parameters, representing our socket endpoint (connectionFd), the buffer for incoming data (timebuffer), and the size of the buffer (maximum amount of data to read, or MAX__BUFFER). The read call returns the number of bytes read from the socket. If a value less than 1 is read, an error has occurred. Succeeding this check, we NULL-terminate our buffer in line 22 and print it in line 23. Our read occurs within a while loop, so we try to read again. At this point, the server has closed the socket (line 42 in Listing 2.1). We’ll detect this close (by a return value of 0 to the read call) and gracefully exit the while loop.

We close our socket endpoint at line 27 (using our socket descriptor, connectionFd) and then exit the client at line 29.

From the source discussion in the client and server, it’s probably become clear that some symmetry exists between the two applications. This topic is explored in the next section.



 < 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