Datagram (UDP) Client

 < Day Day Up > 



Now, let's look at a UDP client implementation using datagram sockets. This implementation uses the Tcl-DP package that can be used to simplify the development of datagram-based applications. Given that it's not possible to identify the source of a datagram through Tcl (nor the Tcl-DP package), the datagram server cannot be written in Tcl without modifications to the Tcl interpreter. The client will be presented, which successfully works with other datagram servers implemented in other languages in this book.

Datagram Client

The datagram client is illustrated in Listing 21.4. We begin by importing our dp (Tcl-DP package) for the script using the package require command. Next, we create a procedure for the datagram client at line 3, calling the new procedure Datagram_Client. This procedure takes two arguments, the address and port to which we want the client to connect (represented by addr and port).

Within Datagram_Client, we create a new datagram socket and connect it to the requested peer using the dp_connect command (line 7). Note that we specify the protocol desired (udp) as well as the host and port arguments passed in by the caller. The newly created datagram socket is stored in the variable dsock. Using the fconfigure command, we configure the new socket for line buffering, which means that we'll read or write lines of text as soon as they're available (line 10). In order to identify ourselves to the remote datagram server, we send a dummy datagram packet to it, which is performed here using the dp_send command, referencing our socket (dsock) and the data to send (here, an ‘a' character). After the server receives our dummy datagram, it will send us a date/time string in a new datagram packet, using the source address of the previously received packet as the destination of the new datagram. We read this new datagram using the dp_recv command, storing the result in variable line (line 16). Finally, we emit the received line to standard-out using the puts command (line 19) and close the socket using the close command at line 22.

The final task for our datagram client is to start it. We call the Datagram_Client at line 31 specifying the host to which we want to connect (localhost) and the port number (13).

Listing 21.4 Tcl Daytime datagram client.

start example
 1   package require dp 4.0  2  3   proc Datagram_Client { addr port } {  4  5     # Create a UDP client socket and connect to the defined  6     # host and port.  7     set dsock [dp_connect udp -host $addr -port $port]  8  9     # Configure for immediate return of data 10     fconfigure $dsock -buffering line 11 12     # Send a dummy packet to the peer 13     dp_send $dsock "a" 14 15     # Read a line from the channel 16     set line [dp_recv $dsock] 17 18     # Emit the line 19     puts $line 20 21     # Close the client socket 22     close $dsock 23 24   } 25 26 27   # 28   # Start the datagram client 29   # 30 31   Datagram_Client localhost 13
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