public static void send() throws IOException {
InetAddress ia = InetAddress.getByName("239.12.2.2");
byte[] data = "Here's some multicast data\r\n".getBytes();
int port = 5555;
byte ttl = 1;
DatagramPacket dp = new DatagramPacket(data, data.length,ia,port);
MulticastSocket ms = new MulticastSocket();
ms.joinGroup(ia);
boolean b = true;
while(b) {
ms.setTimeToLive(ttl);
ms.send(dp);
}
ms.leaveGroup(ia);
ms.close();
}
private static void receiveUDPContent() throws UnknownHostException,
IOException {
InetAddress group = InetAddress.getByName("239.12.2.2");
int port = 5555;
MulticastSocket ms = null;
try {
ms = new MulticastSocket(port);
ms.joinGroup(group);
while (true) {
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
ms.receive(packet);// blocking
byte[] content = packet.getData();
System.err.println(new String (content));
}
} finally {
if (ms != null) {
ms.leaveGroup(group);
ms.close();
}
}
}
public static void send() throws IOException { InetAddress ia = InetAddress.getByName("239.12.2.2"); byte[] data = "Here's some multicast data\r\n".getBytes(); int port = 5555; byte ttl = 1