P2P Programming in JavaThe simplest form of P2P programming in Java employs UDP multicasting, which is datagram packets with a MulticastSocket object. A MulticastSocket requires a multicast IP address (a class D IP address) and a port number. Class D IP addresses fall in the range 224.0.0.0 to 239.255.255.255, though certain addresses are reserved. A peer wishing to subscribe to a multicast group must create a MulticastSocket representing the group and use joinGroup( ) to begin receiving communication. A peer leaves a group by calling leaveGroup( ) or by terminating.
The application described here takes a (welcome) break from accessing/modifying high score lists, which doesn't make for a particularly suitable P2P example. Instead, a MultiTimeServer transmits a packet to a multicast group every second, containing the current time and date. MultiTimeClient objects wait for packets to appear in the group and to print them to standard output. The situation is shown in Figure 29-16. Figure 29-16. UDP multicasting clients and serverThe use of the words "client" and "server" are a little misleading since all the objects involved in the group can potentially send and receive messages. It's my choice to restrict the server to sending and the clients to reading.
The multicasting time serverThe MultiTimeServer object creates a multicast socket for a group at IP address 228.5.6.7, port 6789, and enters a loop which sends a packet out every second: public class MultiTimeServer { private static final String MHOST = "228.5.6.7"; private static final int PORT = 6789; public static void main(String args[]) throws Exception { InetAddress address = InetAddress.getByName(MHOST); MulticastSocket msock = new MulticastSocket(PORT); msock.joinGroup(address); DatagramPacket packet; System.out.print("Ticking"); while(true){ Thread.sleep(1000); // one-second delay System.out.print("."); String str = (new Date( )).toString( ); packet = new DatagramPacket(str.getBytes( ), str.length( ), address, PORT); msock.send(packet); } } } // end of MultiTimeServer class The server is started this way: >java MultiTimeServer Ticking....... The multicasting time clientThe client creates a multicast socket for the same group and enters an infinite loop waiting for packets to appear: public class MultiTimeClient { private static final String MHOST = "228.5.6.7"; private static final int PORT = 6789; public static void main(String args[]) throws IOException { InetAddress address = InetAddress.getByName(MHOST); MulticastSocket msock = new MulticastSocket(PORT); msock.joinGroup(address); byte[] buf = new byte[1024]; DatagramPacket packet = new DatagramPacket(buf, buf.length); String dateStr; while(true){ msock.receive(packet); dateStr = new String( packet.getData( ) ).trim( ); System.out.println(packet.getAddress( ) + " : " + dateStr); } } } A client's execution is: >java MultiTimeClient /172.30.3.176 : Mon Jan 24 16:09:48 ICT 2005 /172.30.3.176 : Mon Jan 24 16:09:49 ICT 2005 /172.30.3.176 : Mon Jan 24 16:09:50 ICT 2005 /172.30.3.176 : Mon Jan 24 16:09:51 ICT 2005 /172.30.3.176 : Mon Jan 24 16:09:52 ICT 2005
|