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 19.3. After importing the Socket module, we create our port variable at line 4, which represents the port to which we’ll bind (as a server socket). Lines 7–9 represent socket creation, using SOCK_DGRAM as the type for a datagram socket. We use the sockaddr_in function at line 12 to create our socket address, using the predefined port variable and the INADDR_ANY constant. Recall that INADDR_ANY specifies that, for a server, we’ll accept incoming connections from any available interface. At line 15, we bind to the previously created packed address structure ($paddr). In this case, there is no call to listen, because this is required only of stream sockets.

The infinite server loop begins at line 17 where we await incoming datagram packets using the recv function (lines 20–21). The packet received is simply a dummy datagram from the client, which gives us the source address of a client that wants to receive the date/time string. We store the return value from recv to $from, which represents the source of the datagram, and then use it at line 27 with the send function. This is a special case of the send function, where the fourth argument represents the destination to which we want to direct the datagram. The datagram was constructed at line 24 using sprintf to construct the string and command scalar localtime to retrieve the date and time in string format.

Listing 19.3 Perl Daytime datagram server.

start example
 1   use Socket;  2  3   # Create the host/port information for the server  4   $port = 13;  5  6   # Create a datagram socket  7   socket( srvSock, Socket::AF_INET,   8            Socket::SOCK_DGRAM, 0 )   9     or die "socket: $!"; 10 11   # Convert the address and port into a packed address 12   $paddr = sockaddr_in( $port, INADDR_ANY ); 13 14   # Bind the server  15   bind( srvSock, $paddr ) or die "bind: $!"; 16 17   while (1) { 18 19     # Receive the dummy datagram from the client 20     $from = recv( srvSock, $line, 1, 0 ) or  21       die "recv: $!"; 22 23     # Get the time and convert it to a string 24     $line = sprintf( "%s\n", scalar localtime ); 25 26     # Write the date/time string to the client 27     send( srvSock, $line, 0, $from ) or  28       die "send: $!"; 29 30   } 31 32   # Close the client socket 33   close( srvSock ) || die "close: $!";
end example

Datagram Client

The datagram client is shown in Listing 19.4, and corresponds with the datagram server previously shown in Listing 19.3.

After importing the Socket module at line 1 using the use statement, we specify the host and port information of the server with which we want to communicate (lines 4–5). Using the socket function (lines 8–10), we create a new datagram socket. At line 13, we convert the destination host address to a numeric binary address using inet_aton, storing the result into $srvaddr. At line 16, we use $srvaddr and our predefined port to create a packed address using sockaddr_in.

Given our new packed address ($paddr), we send our dummy datagram to the server using the send function. Upon the receiving this datagram, the server should respond with the date/time string datagram, which we’ll read here using the recv function. The resulting datagram payload (date and time) is stored into $line at line 23, which is emitted to standard-out at line 26. Finally, we close the client socket at line 29 using the close function.

Listing 19.4 Perl Daytime datagram 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 datagram socket  8   socket( mySock, Socket::AF_INET,   9            Socket::SOCK_DGRAM, 0 ) or  10    die "socket: $!"; 11 12   # Convert the host/address to a numeric address 13   $srvaddr = inet_aton( $host ); 14 15   # Convert the address and port into a packed address 16   $paddr = sockaddr_in( $port, $srvaddr ); 17 18   # Send a dummy packet to identify ourself with  19   # the server 20   send( mySock, " ", 0, $paddr ) or die "send: $!"; 21 22   # Receive the date/time string from the server 23   recv( mySock, $line, 128, 0 ) or die "recv: $!"; 24 25   # Print the received time string 26   print $line, "\n"; 27 28   # Close the client socket 29   close( mySock ) || die "close: $!";
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