Stream (TCP) ServerClient

 < Day Day Up > 



Stream (TCP) Server/Client

The stream server and client demonstrate the Daytime protocol using stream sockets (reliable TCP-based communication).

Stream Server

The Daytime protocol stream server is shown in Listing 19.1. Because all networking applications must have access to the socket module, we first make the socket module visible by including the socket module using the use statement (line 1). At lines 4–5, we specify the host and port variables that define to what we’ll bind (how we’ll advertise the server). At line 8, we create our socket using the socket function. This function looks very similar to the standard function in C, except for the fact that our return server socket is a return argument within the function rather than a return from the function. For the stream socket, we specify the AF_INET domain and the SOCK_STREAM type.

Note 

The die function in Perl (for example, Listing 19.1 line 10) is a simple way to raise an exception. In Listing 19.1, the die function is used in a number of cases to halt script execution if the preceding function fails. Upon failure, the $! symbol is replaced with the string error return, which is emitted to standard-out.

Listing 19.1 Perl Daytime stream server.

start example
  1   use Socket;  2  3   # Create the host/port information  4   $host = INADDR_ANY;  5   $port = 13;  6  7   # Create a new stream socket  8   socket( servSock, Socket::AF_INET,   9            Socket::SOCK_STREAM, 0 )  10     or die "socket: $!"; 11 12   # Create a packed address 13   $paddr = sockaddr_in( $port, $host ); 14 15   # Make the local address reusable 16   setsockopt( servSock, Socket::SOL_SOCKET,  17                Socket::SO_REUSEADDR, 1 ); 18 19   # Bind the local address and enable incoming  20   # connections 21   bind( servSock, $paddr ) or die "bind: $!"; 22   listen( servSock, Socket::SOMAXCONN ) or  23     die "listen: $!"; 24 25   # The big loop 26   while(1) { 27 28     # Accept a client connection 29     if ( accept( cliSock, servSock ) ) { 30 31       # Emit the current time 32       print cliSock scalar localtime, "\n"; 33 34       # Close the client socket 35       close cliSock; 36 37     } 38 39   } 40 41   # Close the server socket 42   close servSock;
end example

At line 13, we create a packed address, which is synonymous with the C sockaddr_in structure. Note that here in Perl, the function to convert an address and a port into the sockaddr_in structure is called sockaddr_in. This function takes a port and host address and returns the packed address. This is used at line 21 to bind the server socket to the local address defined by the packed address (the address we want our server to be advertised as) using the bind function. At line 16, we use the setsockopt function to make the local address reusable, to avoid “address in use” errors. Next, at line 22, we enable our willingness to accept client connections using the listen function.

Our big loop begins at line 26, where we accept client connections and then emit the current date and time to them. At line 29, we accept new client connections using the accept function. The first parameter is the returned client socket descriptor and the second argument is the server socket from which we’ll be accepting connections. For a successful accept (true return), we emit the current date and time to the client using the print function (line 32). The current time is created using scalar localtime, and a newline is appended. The client socket is then closed using the close function (line 35).

Stream Client

Now, let’s look at the Daytime stream client (shown in Listing 19.2). The client creates a socket to connect to the server and then awaits a string to be received through the socket. Upon receiving the string from the server, the message is printed and the client exits.

Listing 19.2 Perl Daytime stream client.

start example
 1   use Socket;  2  3   # Create the host/port information for the server  4   $host = 'localhost';  5   $port = 13;  6  7   # Create a stream socket  8   socket( mySock, Socket::AF_INET,   9             Socket::SOCK_STREAM, 0 ) or  10    die "socket: $!"; 11 12   # Create a packed address 13   $addr = inet_aton( $host ); 14   $paddr = sockaddr_in( $port, $addr ); 15 16   # Connect to the server 17   connect( mySock, $paddr ) or die "connect: $!"; 18 19   # Receive the time string 20   recv( mySock, $buffer, 100, 0 ); 21 22   # Print the received time string 23   print $buffer; 24 25   # Close the client socket 26   close( mySock ) || die "close: $!";
end example

We begin by importing the Socket module at line 1 using the use function. The server to which we want to connect is then identified at lines 4–5 (address and port number). At line 8, we create our client socket using the socket command, specifying the AF_INET domain and SOCK_STREAM type. We then convert our address to a binary address using the inet_aton function. This value is then used at line 14 to create the packed address (using the binary address, addr, and the predefined port). This address is used at line 17 to connect to the server, which returns the new client socket called mySock.

To retrieve the current date and time string, the recv function is used at line 20 to get a line from the socket. This function looks identical to the standard C BSD function. We provide the socket, a buffer to store the data, and an associated length (maximum amount of data to return). The final argument, zero, represents the flags (none are specified). At line 23, the string returned by recv is emitted to standard-out using the print function. Finally, the client socket is closed at line 26 using the close function.



 < 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