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 18.1. As with all Python socket scripts, the socket module must be made visible using the import method (line 1). We also import the time module, in order to gain access to the current time. We create a new server socket at lines 4–5 using the socket method (of the socket module, thus the socket.socket name). As with C applications, we specify AF_INET as the address family (an IP protocol) and SOCK_STREAM to represent a TCP socket. These symbols are prefixed by the socket module, to identify the source module of the symbol.

We utilize a socket option with method setsockopt at lines 6–7 to make the local address reusable. This is useful if the script is stopped or aborted and needs to be restarted quickly. Otherwise, a two-minute wait period is necessary before the local address is available to be bound again. We use a Sockets level option (identified by SOL_SOCKET) for this purpose, specifying the SO_REUSEADDR option to make it reusable.

At line 12, we bind a local name to our socket using the bind method. We specify our server socket instance (servsock) and provide a single argument to bind. This single argument is an address tuple, representing the interface and port from which to accept client connections. In this case, we specify "" as the host, which is the same as specifying INADDR_ANY, or accept client connections on any interface. The port number specified is 13, which is the well-known port for the Daytime protocol. As a final step in server socket setup, we invoke the listen method to enable acceptance of incoming connections. We pass a ‘1’ to the listen method, which specifies that we’ll permit at most one connection awaiting acceptance on the accept queue.

At line 18, we begin the server infinite loop, using the Python while statement. To accept new connections, we use the standard socket accept method. This method is used solely by stream sockets and blocks until a client connects. After a client connects to the server, a new client socket is created and returned. At line 21, the accept method returns the new client socket to the clisock object. The accept method also returns address information about the client in the second returned parameter, in this case addr. This address tuple contains the remote client address and port number.

At line 24, we collect the current time using the time module. The time method returns the current time as the number of seconds since a given epoch (January 1st, 1970). The ctime method of the time module converts this quantity into a string representing the local time. This time string (tm) is then shipped to the client socket using the send method (line 27) with the instance of the client socket, clisock. The close method, at line 30, then closes the client socket and control ultimately returns to line 21, where we await a new client connection.

Listing 18.1  Python Daytime stream server.

start example
 1   import socket, time  2  3   # Create a new stream socket  4   servsock = socket.socket( socket.AF_INET,   5                             socket.SOCK_STREAM )  6  7   # Make the address/port combination reusable  8   servsock.setsockopt( socket.SOL_SOCKET,   9                        socket.SO_REUSEADDR, 1 ) 10 11   # Bind our server socket to port 13 12   servsock.bind( ("", 13) ) 13 14   # Mark the server's willingness to accept client connections 15   servsock.listen( 1 ) 16 17   # The big loop 18   while 1: 19 20     # Accept a new client connection 21     clisock,addr = servsock.accept() 22 23     # Collect the current time 24     tm = time.ctime( time.time() ) + "\n" 25 26     # Send the time to the client 27     clisock.send( tm ) 28 29     # Close the client connection 30     clisock.close()
end example

Stream Client

Now, let’s look at the Daytime stream client (shown in Listing 18.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 18.2  Python Daytime stream client.

start example
  1   import socket  2  3   # Create a new stream socket  4   sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )  5  6   # Connect to the daytime port  7   sock.connect( ("localhost", 13) )  8  9   # Receive the time/date information 10   ts = sock.recv( 100 ) 11 12   # Emit the received string 13   print ts 14 15   # Close the client socket 16   sock.close()

We begin by importing the socket module at line 1 and then creating a new stream socket at line 4 using the socket method from the socket module. At line 7, we create a connection to the client using the connect method. To connect, we pass our address tuple that specifies the localhost interface and the Daytime service port number, 13. After the connection is made, we read the current time through the socket using the recv method (line 10). We specify the largest size that we’ll read from the socket (100 octets), but in fact, we’ll actually read much less. The resulting time string is returned and stored into the new object ts. At line 13, we print the time using the print statement, and, finally, we close the socket at line 16 using the close method.

end example



 < 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