Multicast ServerClient

 < Day Day Up > 



Multicast Server/Client

The multicast server and client are fundamentally derivatives of the datagram code patterns, though we use another API because Perl does not natively support sending multicast packets.

Multicast Server

The Perl language source code for the multicast server is shown in Listing 19.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.

To send datagrams, no multicast support is actually needed. All we do is send datagrams to an address that is a multicast address. In order to receive multicast datagrams, we must join the specific multicast group.

At line 1, we import the standard Socket module using the use statement. Next, we create the group and port variables at lines 4–5. Note that instead of a host address, we define a group variable, which contains a multicast address. At lines 8–10, we create our socket that is a standard datagram socket (we’ll note a difference between this and the multicast client in Listing 19.6). In order to bind to the address and port, we use the SO_REUSEADDR socket option to avoid the common “address in use” error. At line 16, we create the packed address using sockaddr_in and then bind ourselves to this address (not completely necessary in this example).

To direct our datagrams, we need a packed address structure containing the destination address (group multicast address) and port. This is accomplished with lines 20 and 22, where we first create a numeric version of our group address using inet_aton, and then use sockaddr_in to create a packed address with the port number.

Our infinite loop begins at line 24. We create a time string at line 26, and then send the string to the group as a datagram payload using the send function. Note that we specify not only our server socket and date/time string buffer, but also the packed address representing the multicast group ($paddr). Finally, at line 30, we sleep for one second and then repeat the process.

Listing 19.5 Perl Daytime multicast server.

start example
 1   use Socket;  2  3   # Create the host/port information for the server  4   $group = '239.0.0.2';  5   $port = 45002;  6  7   # Create a new multicast socket  8   socket( srvSock, Socket::AF_INET,   9            Socket::SOCK_DGRAM, 0) 10     or die "socket: $!"; 11 12   # Make the local address reusable 13   setsockopt( srvSock, Socket::SOL_SOCKET, 14                Socket::SO_REUSEADDR, 1 ); 15 16   $myaddr = sockaddr_in( $port, INADDR_ANY ); 17 18   bind( srvSock, $myaddr ); 19 20   $srvaddr = inet_aton( $group ); 21 22   $paddr = sockaddr_in( $port, $srvaddr ); 23 24   while( 1 ) { 25 26     $line = sprintf( "%s\n", scalar localtime ); 27 28     send( srvSock, $line, 0, $paddr ) or die "send: $!"; 29 30     sleep( 1 ); 31 32   } 33 34   close( srvSock ) or die "close: $!";
end example

Multicast Client

The Perl source code for the multicast client is shown in Listing 19.6. The multicast client includes a number of new features not previously discussed, but reflects symmetry with the multicast server, as seen with other code patterns.

In the case of the multicast client, the Multicast API must be used. We import not only the standard Socket module at line 1, but also the Multicast module at line 2 (also called IO::Socket::Multicast). In lines 5–6, we specify our group multicast address and port to which we want to subscribe. At line 9, we create a new multicast socket using the new function of the Multicast module. We specify the port to which we want to bind (LocalPort), the protocol of the socket (UDP) and type (SOCK_DGRAM), and finally an option that we want enabled (Reuse, or SO_REUSEADDR). This is an alternate mechanism for socket creation that can simplify a number of operations into a single call.

Now that we have our multicast datagram socket, we need to join the multicast group in order to receive multicast datagrams. At line 17, we use the mcast_add function to join the group. This is synonymous with the IP_ADD_MEMBERSHIP socket option. After this function completes, we’re subscribed to the multicast group and can receive multicast datagrams. We call the recv function to receive a datagram, at line 20, and the result is stored in $line. We emit this to standard-out at line 23 using the print function and, finally, drop ourselves from the multicast subscription using the mcast_drop function. This command is the same as using the IP_DROP_MEMBERSHIP socket option in the standard BSD API.

Listing 19.6 Perl Daytime multicast client.

start example
 1   use IO::Socket;  2   use IO::Socket::Multicast;  3  4   # Create the host/port information for the server  5   GROUP = '239.0.0.2';  6   PORT = '45002';  7  8   # Create a new multicast socket  9   my $sock = IO::Socket::Multicast->new(  10                                LocalPort=>PORT,  11                                Proto=>'udp',  12                                Type=> SOCK_DGRAM,  13                                Reuse => 1 )  14              or die "new mcast socket $!"; 15 16   # Join the predefined multicast group 17   $sock->mcast_add( GROUP ) or die "mcast_add $!"; 18 19   # Receive the date/time string 20   $sock->recv( $line, 128 ); 21 22   # Emit it to standard-out 23   print $line; 24 25   # Remove ourselves from the multicast group 26   $sock->mcast_drop( GROUP ) or die "mcast_drop $!";
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