01: //***************** Multicast Time Server *************************** 02: import java.net.InetAddress; 03: import java.net.MulticastSocket; 04: import java.net.DatagramPacket; 05: import java.util.Date; 06: import java.io.IOException; 07: 08: public class TimeServerMC { 09: public static void main(String[ ] args) { 10: try { 11: // Join group at network address. 12: InetAddress t_group = InetAddress.getByName(addr); 13: MulticastSocket mcs = new MulticastSocket(port); 14: mcs.joinGroup(t_group); 15: 16: DatagramPacket t_packet = null; 17: 18: // Multicast the time every 10 seconds. 19: while (true) { 20: byte[ ] t_bytes = 21: new Date().toString().getBytes(); 22: t_packet = new DatagramPacket(t_bytes, 23: t_bytes.length, t_group, port); 24: mcs.send(t_packet); 25: Thread.sleep(winks); 26: } 27: } 28: catch(IOException e) { System.err.println(e); } 29: catch(InterruptedException e) { System.err.println(e); } 30: } 31: private static final int port = 9977; 32: private static final String addr = "228.1.2.3"; 33: private static final int winks = 10000; // milliseconds 34: }