import java.net.*;
import java.io.*;
public class MultiApp {
public static void main( String [] args ) throws Exception {
String myHost = "localhost";
int myPort = 6789; // echo
MulticastSocket ms = new MulticastSocket(6789);
InetAddress group = InetAddress.getByName("228.5.6.7");
ms.joinGroup( group );
while ( true ) {
String message = new java.util.Date().toString();
byte [] data = message.getBytes();
DatagramPacket pack = new DatagramPacket(data, data.length,
group, myPort );
ms.send( pack, (byte)42 );
pack = new DatagramPacket(new byte [1024], 1024);
ms.receive( pack );
System.out.println( "Echo from: " +
pack.getAddress().getHostName() + " - "
+ new String( pack.getData() ) );
ms.receive( pack );
System.out.println( "Echo from: " +
pack.getAddress().getHostName() + " - "
+ new String( pack.getData() ) );
Thread.sleep(2000);
}
}
}
|