| < Day Day Up > |
|
The broadcast server and client, like the multicast server and client, are fundamentally derivatives of the datagram code patterns. This is again because broadcast communication is based upon the datagram model.
The Perl source code for the broadcast server is shown in Listing 19.7. The server is broadcast-based, which means a single message emitted by the server is received by every client that is configured to receive broadcast datagrams (for the given port).
At line 1, we import the Socket module and then define the host and port to which we’ll direct our datagrams (lines 4–5). Note that the $host variable is a broadcast address, but this could be further constrained given the subnet in which it’s executed (for example, “192.168.1.255”). At line 7, we create our datagram socket using the socket function and then enable sending of broadcast datagrams at lines 13–14 using the setsockopt function with the SO_BROADCAST option.
At line 18, we use inet_aton to convert the broadcast address from dotted-string notation to a binary address. This is then stored, with the port, into a packed address using the sockaddr_in function. The remainder of this pattern is similar to the datagram server, except that we don’t wait for dummy datagrams from clients (although, it is identical to the multicast server pattern). In the big loop (lines 23–32), we create a new date/time string using sprintf and scalar localtime, and then emit it out using the send function. Any socket currently bound to the broadcast address and port will then receive the datagram.
Listing 19.7 Perl Daytime broadcast server.
1 use Socket; 2 3 # Create the host/port information for the server 4 $host = '255.255.255.255'; 5 $port = 45003; 6 7 # Create a datagram socket 8 socket( srvSock, Socket::AF_INET, 9 Socket::SOCK_DGRAM, 0 ) or 10 die "socket: $!"; 11 12 # Make the socket broadcast-send capable 13 setsockopt( srvSock, Socket::SOL_SOCKET, 14 Socket::SO_BROADCAST, 1 ) or 15 die "setsockopt: $!"; 16 17 # Convert the host/address to a numeric address 18 $srvaddr = inet_aton( $host ); 19 20 # Convert the address and port into a packed address 21 $paddr = sockaddr_in( $port, $srvaddr ); 22 23 while ( 1 ) { 24 25 $line = sprintf( "%s\n", scalar localtime ); 26 27 send( srvSock, $line, 0, $paddr ) or 28 die "send: $!"; 29 30 sleep( 1 ); 31 32 } 33 34 # Close the client socket 35 close( srvSock ) || die "close: $!";
The Perl source code for the broadcast client is shown in Listing 19.8. As with the broadcast server, we concentrate on the differences to the standard datagram client.
The broadcast client pattern is identical to the datagram client, except for the difference in host addresses (in this case, the broadcast address). We begin at line 1 by importing the Socket module and then defining the host and port variables with the broadcast address and selected port. At line 8, we create the datagram socket using the socket function (type SOCK_DGRAM).
Next, we create a binary address of the dotted-notation string IP address using inet_aton (line 13) and then create our packed address structure with the binary address ($srvaddr) and the port to create $paddr (line 16). This packed address is then used at line 19 with bind to bind the socket to the local address.
Recall that broadcast datagrams are received by all sockets that are bound to the broadcast address. Therefore, when we call recv at line 22, we’ll receive any broadcast datagram just like any other client bound to the same address (on the same host, or different host on the subnet). We emit the received date/time string ($line) to standard-out using the print function and then close the socket at line 28 using the close function.
Listing 19.8 Perl Daytime broadcast client.
1 use Socket; 2 3 # Create the host/port information for the server 4 $host = '255.255.255.255'; 5 $port = 45003; 6 7 # Create a datagram socket 8 socket( cliSock, 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 # Bind to the broadcast address and port 19 bind( cliSock, $paddr ) or die "bind: $!"; 20 21 # Retrieve the time/date string from the socket 22 recv( cliSock, $line, 128, 0 ) or die "recv: $!"; 23 24 # Emit the line to standard-out 25 print $line; 26 27 # Close the client socket 28 close( cliSock ) || die "close: $!";
| < Day Day Up > |
|