| < 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 Ruby source code for the broadcast server is shown in Listing 20.8. 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).
Broadcast communication is datagram-specific, and, therefore, we begin at line 4 by creating a datagram socket using UDPSocket::new. In order to send broadcast datagrams, we use the setsockopt method and enable the SO_BROADCAST socket option (lines 8 and 9). For debugging purposes only, we emit the server address information at lines 11 and 12.
We begin our infinite loop at line 15, where the broadcast server will emit broadcast datagrams once per second. We begin at line 18 by creating a new time string using the Time::new::asctime method. This returns a string representing a human-readable date and time string, to which we append a newline. We then use the send method at line 21 to emit our datagram. We specify our data (ts), our flags (0), the limited broadcast address (“255.255.255.255”), and our chosen broadcast port, 45003. Note that this is the limited broadcast address because it is limited to the local LAN and won’t be forwarded. Finally, at line 24, we sleep for one second and then start the time emission process all over again.
Listing 20.8 Ruby Daytime broadcast server.
![]() |
1 require ‘socket’ 2 3 # Create a new UDP Socket 4 servsock = UDPSocket::new(Socket::AF_INET) 5 6 # Allow sending of broadcast datagrams. 7 servsock::setsockopt( Socket::SOL_SOCKET, 8 Socket::SO_BROADCAST, 1 ) 9 10 # Debug data -- emit the server socket info 11 print("server address : ", 12 servsock.addr::join(":"), "\n") 13 14 # The big loop 15 while true 16 17 # Create a time string with the current time 18 ts = Time::new::asctime + "\n" 19 20 # Emit the time through the socket to the client 21 servsock.send( ts, 0, "255.255.255.255", 45003 ) 22 23 # Sleep for 1 seconds 24 sleep 1 25 26 end
![]() |
The Ruby source code for the broadcast client is shown in Listing 20.9. As with the broadcast server, we concentrate on the differences to the standard datagram client.
We begin again by creating our datagram socket at line 4 and then bind the broadcast address and port to it using the bind method at line 7. We bind our client socket to the broadcast address and port because the client must receive broadcast datagrams. Note that the server does not use bind. If the server were to receive broadcast datagrams, it would be required to bind as the client does. At line 10, we receive the broadcast datagram using the recvfrom method. We could have used the recv method here to simplify the client. At line 13, we emit the time string received by the server using the print method and then, finally, at line 16, we close the client socket using the close method.
Listing 20.9 Ruby Daytime broadcast client.
![]() |
1 require ‘socket’ 2 3 # Create a new UDP Socket 4 clisock = UDPSocket::new(Socket::AF_INET) 5 6 # Bind the server socket to the daytime port 7 clisock.bind("255.255.255.255", 45003) 8 9 # Receive a datagram through the socket 10 reply, from = clisock.recvfrom(100, 0) 11 12 # Print the received datagram payload 13 print reply 14 15 # Close the client socket 16 clisock.close
![]() |
| < Day Day Up > |
|