Broadcast (UDP) Client

 < Day Day Up > 



Now, let's look at a UDP broadcast client implementation using datagram sockets. The broadcast client, like the multicast client, is fundamentally a derivative of the datagram code patterns. This is again because broadcast communication is based upon the datagram model. Only the broadcast client is shown because the broadcast server cannot be written in Tcl, or the Tcl-DP package. This is because the APIs lack the ability to set the broadcast socket option that makes it possible to send broadcast datagrams. An application is still able to receive broadcast datagrams without this option.

The broadcast client in Tcl is shown in Listing 21.7. As with all of the Tcl-DP scripts, line 1 begins with the importing of the Tcl-DP package. We then declare our Broadcast_Client procedure, which accepts an address and port to which the procedure will bind (similar to the multicast join). We use the dp_connect command with the udp protocol at line 7 to create the client socket, and bind it to the broadcast IP address and port number defined by the caller (addr and port). The final setup element utilizes fconfigure to enable line buffering on the client socket (line 10).

The dp_recv command is used to read a line from the socket, which is stored in variable line (line 13). At line 16, the line is emitted to standard-out using the puts command. The broadcast datagram socket is then closed at line 19 using the close command.

The broadcast datagram client is started at line 28. This is done by calling the Broadcast_Client procedure with the broadcast address and the port number from which we want to receive datagrams.

Listing 21.7 Tcl Daytime broadcast client.

start example
 1   package require dp 4.0  2  3   proc Broadcast_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 -myaddr $addr -myport $port]  8  9     # Configure for immediate return of data 10     fconfigure $dsock -buffering line 11 12     # Read a line from the channel 13     set line [dp_recv $dsock] 14 15     # Emit the line 16     puts $line 17 18     # Close the client socket 19     close $dsock 20 21   } 22 23 24   # 25   # Start the datagram client 26   # 27 28   Broadcast_Client 255.255.255.255 45003
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