| < Day Day Up > |
|
In many other languages, the multicast server and client code are derivatives of the datagram code patterns. Java differs here because multicast sockets are implemented using its own MulticastSocket class. The MulticastSocket class is derived from the DatagramSocket class, so it inherits the functionality provided there.
The Java language source code for the multicast server is shown in Listing 17.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.
Because multicast communication has been discussed in previous chapters, we’ll forgo discussion of multicast specifics and concentrate solely on the methods that Java provides to achieve multicast communication.
To set up our multicast server (lines 4 and 5), we create our mcastsrv class (line 5) and the Java application’s main method (line 7). We create our datagram packet in lines 13–14, associated with a byte[] payload of 100 octets. At line 19, the multicast socket is created using the MulticastSocket constructor. No port is specified with the constructor because we intend not to receive through this socket, but only to send. Next, we create an InetAddress object for our multicast group address “239.0.0.2”, using the getByName method. We then join this multicast group using the joinGroup method of the MulticastSocket class.
Our setup is complete at line 31, and we enter the big loop to distribute the multicast packets containing the date and time string. In each iteration through the loop, we create an anonymous Date object and convert it to a String (line 34). We then convert it to a byte[] with getBytes and store it in the buffer. Lines 39–42 create a new DatagramPacket and store not only the date/time string buffer, but also the InetAddress of the multicast group and port. At line 47, we send the multicast packet using the send method. Finally, at line 51, we invoke the sleep method of the Thread class to sleep for one second. Upon completion of the sleep, we continue the loop and send another date/time datagram to the multicast group.
Listing 17.5 Java Daytime multicast server.
1 import java.net.*; 2 import java.io.*; 3 import java.util.*; 4 5 public class mcastsrv { 6 7 public static void main( String[] args ) { 8 9 String group = "239.0.0.2"; 10 int port = 45002; 11 MulticastSocket socket; 12 byte[] buffer = new byte[100]; 13 DatagramPacket packet; 14 15 16 try { 17 18 // Create the multicast socket 19 socket = new MulticastSocket(); 20 21 try { 22 23 // Get a binary address for the group 24 InetAddress adrs = 25 InetAddress.getByName( group ); 26 27 // Join the multicast group 28 socket.joinGroup( adrs ); 29 30 // The big loop 31 while ( true ) { 32 33 // Create the time/date string 34 String theDate = new Date().toString(); 35 buffer = theDate.getBytes(); 36 37 // Create a new datagram packet using the 38 // date/time string. 39 packet = new DatagramPacket( buffer, 40 buffer.length, 41 adrs, port ); 42 43 try { 44 45 // Try to send the multicast packet to the 46 // client(s). 47 socket.send( packet ); 48 49 // Sleep for one second between sends 50 try { 51 Thread.sleep( 1000 ); 52 } 53 catch( InterruptedException e ) 54 { 55 // for Thread sleep 56 System.out.println( e ); 57 } 58 59 } 60 catch( IOException e ) 61 { 62 // for socket send 63 System.out.println( e ); 64 } 65 66 } 67 68 } 69 catch( IOException e ) 70 { 71 // for getByName 72 System.out.println( e ); 73 } 74 75 } 76 catch( IOException e ) 77 { 78 // for socket creation 79 System.out.println( e ); 80 } 81 82 } 83 84 }
The Java source code for the multicast client is shown in Listing 17.6. The multicast client reflects symmetry with the multicast server, as seen with other code patterns.
After making the necessary package classes visible (lines 1–2), the Java class and main method for the application are defined (lines 4 and 6). The multicast group address is defined at line 8, initially as a String, but this will be converted to an InetAddress later. We also create our DatagramPacket and, using the constructor, initialize it with our byte[] buffer.
At line 18, we create our multicast socket using the MulticastSocket constructor. We also specify our desired multicast port in the constructor. At lines 23–24, we convert our multicast group address (in String dotted-notation) to an InetAddress object using the getByName method. This new object is used at line 28 to join the multicast group using the joinGroup method. We then enter our loop at line 32 to receive the multicast datagrams. At line 34, the receive method is invoked with our multicast socket to receive available datagrams. Upon return, we convert the byte[] payload of the datagram to a String using the getData method. The String is then emitted to standard-out at line 37.
Listing 17.6 Java Daytime multicast client.
1 import java.net.*; 2 import java.io.*; 3 4 public class mcastcli { 5 6 public static void main( String[] args ) { 7 8 String group = "239.0.0.2"; 9 int port = 45002; 10 MulticastSocket socket; 11 byte[] buffer = new byte[100]; 12 DatagramPacket packet = 13 new DatagramPacket( buffer, buffer.length ); 14 15 try { 16 17 // Create a new client socket 18 socket = new MulticastSocket( port ); 19 20 try { 21 22 // Get the address of the current host 23 InetAddress grpaddr = 24 InetAddress.getByName( group ); 25 26 try { 27 28 socket.joinGroup( grpaddr ); 29 30 try { 31 32 while( true ) { 33 34 socket.receive( packet ); 35 String received = 36 new String( packet.getData() ); 37 System.out.println( received ); 38 39 } 40 41 } 42 catch( SocketException e ) 43 { 44 System.out.println( e ); 45 } 46 catch( IOException e ) 47 { 48 System.out.println( e ); 49 } 50 51 } 52 catch( IOException e ) 53 { 54 System.out.println( e ); 55 } 56 57 } 58 catch( UnknownHostException e ) 59 { 60 System.out.println( e ); 61 } 62 63 } 64 catch( IOException e ) 65 { 66 System.out.println( e ); 67 } 68 69 } 70 71 }
| < Day Day Up > |
|