Datagram (UDP) ServerClient

 < Day Day Up > 



Datagram (UDP) Server/Client

Now, let’s look at a UDP server and client implementation using datagram sockets. Recall that datagram communication is not connection-oriented; therefore, each datagram that we send must also include the intended recipient.

Datagram Server

The datagram server is illustrated in Listing 18.3. After making the socket and time module visible using the import statement, we create our datagram socket using the socket.socket method (line 4). To create a datagram socket, we specify that the family will be AF_INET (for Internet protocols) and that the socket type will be SOCK_DGRAM (for the datagram protocol). The result of the socket.socket method is a new datagram socket, stored in servsock. We use the setsockopt method (at line 7–8) to permit the local address information to be reused. As a final step in our server setup, the bind method is called to bind a local name to the socket (line 11). We use the address represented by "" to permit incoming client connections on any interface and specify the Daytime service port, 13. At this stage, our server is ready to accept client connections.

Listing 18.3 Python Daytime datagram server.

start example
 1   import socket, time  2  3   # Create a new datagram socket  4   servsock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )  5  6   # Make the address/port combination reusable  7   servsock.setsockopt( socket.SOL_SOCKET,   8                         socket.SO_REUSEADDR, 1 )  9 10   # Bind our server socket to INADDR_ANY and port 13 11   servsock.bind( ("", 13) ) 12 13   # The big loop 14   while 1: 15 16     # Receive a client datagram 17     data, addr = servsock.recvfrom( 0 ) 18 19     # Collect the current time 20     tm = time.ctime( time.time() ) + "\n" 21 22     # Send the time to the client 23     servsock.sendto( tm, addr )
end example

At line 14 in Listing 18.3, we start our infinite loop. We await a zero-length datagram from a client at line 17 using the recvfrom method. This method returns two items, the datagram’s data buffer (data) and the source address of the datagram (addr). At line 20, we create a new time string (identically to the stream server shown in Listing 18.1). Finally, at line 23, we send the time string to the client using the sendto method. The destination of the time datagram is the source of our initial zero-length datagram (stored as addr).

Datagram Client

The datagram client is shown in Listing 18.4, and corresponds with the datagram server previously shown in Listing 18.3.

After making the socket module visible at line 1 using the import statement, we create an address tuple that represents the server to which we’ll communicate. We create our client socket at line 7, specifying SOCK_DGRAM for a datagram protocol socket. As a final setup step, we bind our client socket with a local address using the bind method (line 10). Although this step is unnecessary, it’s done here to demonstrate another element of the bind method. We provide the wildcard address (interface) as the first element of the tuple and the second element representing the port number. If an IP address (or host name) had been specified, from the client perspective, this means that our client could communicate only through that interface. This in essence restricts communication to a specific interface, rather than allowing the routing tables to make this decision. Further, the port number specified is 0, which is not a legal port number. The zero port number tells the stack that an ephemeral (dynamic) port should be created for the client. If the script had not performed a bind, this exact process would have been done automatically. We provide this example here to illustrate how the bind can be used in a client setting.

Listing 18.4 Python Daytime datagram client.

start example
 1   import socket  2  3   # Create the destination address structure  4   addr = 'localhost', 13  5  6   # Create a new stream socket  7   sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )  8  9   # Bind to the INADDR_ANY and the ephemeral port 10   sock.bind( ("", 0) ) 11 12   # Send our blank datagram to notify the server that we 13   # request a time string response. 14   sock.sendto( "", addr ) 15 16   # Receive the time/date information 17   ts = sock.recv( 100 ) 18 19   # Emit the received string 20   print ts 21 22   # Close the client socket 23   sock.close()
end example

To begin the datagram communication, we send an empty datagram to the server at line 14. We use the sendto method because with datagram communication, we need to specify the destination address of the datagram. The first parameter to sendto is our empty datagram and the second is our address tuple (that was created earlier at line 4). After the server receives our empty datagram, it sends us a datagram containing the time string. We read this using the recv method (line 17), specifying a size (the maximum datagram that is to be received). The resulting string is stored into the string object ts. At line 20, we emit the time string using the print statement and then close the socket at line 23 using the close method.



 < 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