//************ client for time uses multicast datagram ******** import java.net.MulticastSocket; import java.net.DatagramPacket; import java.net.InetAddress; import java.util.Date; import java.io.IOException; public class TimeReceiverMC { public static void main(String[ ] args) { try { // Join multicast group. InetAddress t_group = InetAddress.getByName(addr); MulticastSocket mcs = new MulticastSocket(port); mcs.joinGroup(t_group); // Prepare a packet to receive data. byte[ ] t_bytes = new Date().toString().getBytes(); DatagramPacket packet = new DatagramPacket(t_bytes, t_bytes.length); // Receive 10 packets and then leave the group; for (int i = 0; i < 10; i++) { mcs.receive(packet); System.out.println("From group: " + new String(packet.getData())); } mcs.leaveGroup(t_group); } catch(IOException e) { System.err.println(e); } } private static final int port = 9977; private static final String addr = "228.1.2.3"; }