Broadcast ServerClient

 < Day Day Up > 



Broadcast Server/Client

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.

Broadcast Server

The Python source code for the broadcast server is shown in Listing 18.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).

After importing the socket and time modules (line 1), we create our datagram socket using the socket method from the socket module (line 4). At lines 7–8, we enable the SO_BROADCAST socket option. This permits us to send datagrams to the broadcast address. Finally, ending the server setup, we use the bind method to retrieve an ephemeral port for the socket.

At line 14, we create an address tuple that represents the broadcast group to which we’ll communicate. The address represents the broadcast address (using the special <broadcast> symbol and port 45003). Clients must bind this address tuple in order to receive datagrams from the server.

Listing 18.7 Python Daytime broadcast server.

start example
  1   import socket, time  2  3   # Create a new datagram socket  4   servsock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )  5  6   # Enable the ability to send to the broadcast address  7   servsock.setsockopt( socket.SOL_SOCKET,   8                        socket.SO_BROADCAST, 1 )  9 10   # Bind our server socket to INADDR_ANY and an ephemeral port 11   servsock.bind( ("", 0) ) 12 13   # Create the destination address structure 14   addr = '<broadcast>', 45003 15 16   # The big loop 17   while 1: 18 19     # Collect the current time 20     tm = time.ctime( time.time() ) + "\n" 21 22     # Send the time to the client 23     servsock.sendto( tm, addr ) 24 25     # Wait one second before sending another time string 26     time.sleep(1)
end example

We begin our infinite server loop at line 17. Iteration through the loop begins by constructing a new time string using the time module. The time string is stored in the string variable tm. At line 23, we send the current time string to the broadcast address tuple (created previously at line 14) using the sendto method. Finally, we use the sleep method of the time module (line 26) to stall for one second and then repeat the process again.

Broadcast Client

The Python source code for the broadcast client is shown in Listing 18.8. As with the broadcast server, we concentrate on the differences to the standard datagram client.

Listing 18.8 Python Daytime broadcast client.

start example
 1   import socket  2  3   # Create a new stream socket  4   sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )  5  6   # Bind to the INADDR_ANY and our broadcast port  7   sock.bind( ("", 45003) )  8  9   # Receive the time/date information 10   ts = sock.recv( 100 ) 11 12   # Emit the received string 13   print ts 14 15   # Close the client socket 16   sock.close()
end example

The broadcast client is a very simple script that receives a time string datagram from the broadcast server and then exits. After importing the socket module at line 1, we create our datagram socket at line 4 (identically to other datagram sockets). At line 7, we bind the client socket to the broadcast port and the wildcard interface (INADDR_ANY, represented by ""). At line 10, we receive the time string datagram using the recv method. We could have used the recvfrom method, but in this case, we’re not interested in the source of the datagram. We specify a maximum of 100 bytes to be received from the server, which, in this case, is much larger than is necessary. At line 13, we emit the time string using the print statement and finally close the client socket at line 16 using the close method.



 < 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