Multicast ServerClient

 < Day Day Up > 



Multicast Server/Client

The multicast server and client are basic derivatives of the datagram code patterns, using the Tcl-DP package. This is primarily because multicast communication is based upon the datagram model and because Tcl does not support multicast (or datagrams) natively.

Multicast Server

The Tcl language source code for the multicast server is shown in Listing 21.5. Recall that multicast communication is group-based; therefore, a single message emitted by the server is received by every client that is currently a member of the multicast group.

Listing 21.5 Tcl Daytime multicast server.

start example
 1   package require dp 4.0  2  3  4   proc sleep { value } {  5     set temp 0  6     after $value {set temp 1}  7     vwait temp  8   }  9 10 11   # 12   #  Multicast Server setup 13   # 14   proc Multicast_Server { addr port } { 15 16     # Create the multicast socket 17     set mcsock [dp_connect ipm -group $addr -myport $port] 18 19     # Configure the socket so that each puts results 20     # in a socket send. 21     fconfigure $mcsock -buffering line 22 23     while (1) { 24 25       # Get the current time in seconds 26       set clock_val [ clock seconds ] 27 28       # Convert the seconds time into string 29       set date_str [ clock format $clock_val ] 30 31       # Emit the date/time string to the socket 32       puts $mcsock $date_str 33 34       sleep 1000 35 36     } 37 38   } 39 40 41   # 42   # Start the multicast server 43   # 44 45   Multicast_Server 239.0.0.2 45002
end example

The multicast server, like the datagram client, is written as a simple procedure that is called with the address and port for which to register as a server. At line 1, we import the Tcl-DP package so that we can create multicast datagram sockets.

A sleep procedure is constructed using the after and vwait commands. The after command specifies that we'll delay the number of milliseconds specified in the input value to the procedure, and upon completion of that delay, perform the command that follows (in this case, set the temp value to 1). The procedure continues to the vwait command, where we turn control over to Tcl's event loop. The vwait command awaits a change of the temp variable, which is set by the after command (after the delay completes). Therefore, once the variable temp changes, the vwait command completes and permits execution to continue.

The Multicast_Server procedure is created next (line 14), with two arguments specifying the host and port to which the server must locally bind. At line 17, we create the multicast socket using the dp_connect command, specifying the protocol type (ipm, for IP multicast), the group address, and the port. After the multicast server socket is created, the resulting socket is stored in mcsock. The fconfigure command is then called to allow data to be immediately sent upon puts (line 21). We then begin our infinite loop at line 23, and generate the date and time string at lines 26 and 29 (resulting in the date_str). At line 32, we use the puts command to write our date_str to the socket to be multicast to anyone else happening to have joined the multicast group. Our previously discussed sleep procedure is then called, with an argument of 1000 ms (1 second).

Note 

The usage of dp_connect here shouldn't be confused with the standard connect call of the BSD API. In this usage, the dp_connect call provides the bind functionality, binding the local group address and port to the newly created socket. This functionality could also be used to create a datagram server socket.

Finally, at line 45, we call the Multicast_Server procedure and pass to it the group address we want to join (239.0.0.2) and the port (45002).

Multicast Client

The Tcl source code for the multicast client is shown in Listing 21.6. This source illustrates symmetry with the previously discussed multicast server, shown in Listing 21.5.

Listing 21.6 Tcl Daytime multicast client.

start example
 1   package require dp 4.0  2  3   #  4   #  Multicast Client setup  5   #  6   proc Multicast_Client { addr port } {  7  8     # Create the multicast socket  9     set mcsock [dp_connect ipm -group $addr -myport $port] 10 11     # Configure the socket so that each puts results 12     # in a socket send. 13     fconfigure $mcsock -buffering line 14 15     # Read a line from the channel 16     set line [dp_recv $mcsock] 17 18     # Emit the date/time string to the socket 19     puts $line 20 21     # Close the client socket 22     close $mcsock 23 24   } 25 26 27   # 28   # Start the multicast client 29   # 30 31   Multicast_Client 239.0.0.2 45002
end example

At line 1, we import the Tcl-DP package for access to multicast sockets. At line 6, we create our Multicast_Client procedure that accepts two arguments, the host and port to which we'll bind (or in this case, the group to which we'll join). This is done using the dp_connect call with the ipm argument specifying that we're creating an IP multicast socket. We configure our socket for line buffering at line 13 using the fconfigure command and then read in a line of text from the socket at line 16 using dp_recv. This line of text (the date and time string) are emitted to standard-out using the puts command (line 19), and, finally, the socket is closed at line 22 using the close command.

The multicast client is started at line 31 with a call to the Multicast_Client procedure. As shown at line 6, the procedure takes two arguments, the group address and port to which we'll bind. At line 31, we specify our group address as 239.0.0.2 (a multicast IP address) and port 45002.



 < 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