public class FileMulticastClient {
public static void main(String[] args) throws IOException, InterruptedException {
InetAddress groupAddress = InetAddress.getByName("239.0.0.1");
MulticastSocket receiveSocket = new MulticastSocket(9999);
receiveSocket.joinGroup(groupAddress);
byte[] buffer = new byte[1024];
while(true){
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
receiveSocket.receive(packet);
Thread.sleep(1000);
String s = new String(packet.getData(), 0, packet.getLength());
System.out.println(s);
}
}
}
public class FileMulticastServer {
public static void main(String[] args) throws IOException {
InetAddress groupAddress = InetAddress.getByName("239.0.0.1");
MulticastSocket sendSocket = new MulticastSocket(0);
while(true){
String str = "qweqweqwe";
byte[] buffer = str.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length,
groupAddress,9999);
sendSocket.send(datagramPacket);
}
}
}
|