I've written some code that is suppose to receive data from a MulticastSocket. For some reason, the socket will never receive. I've broken this down to the code below. I send several packets but those packets are never received. I can confirm that the packets are send because wireshark detects them. Further, I've send the packets from a different machine (same network) and still the packets are never received. I have confirmed the firewall is off, any ideas?
@Test
public void test_testrun() {
InetAddress addr;
try {
addr = InetAddress.getByName("234.67.67.67");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
int port = 5555;
byte[] data = "1234567890".getBytes();
final DatagramPacket sendPacket = new DatagramPacket(
data,
data.length,
addr,
port
);
// initialize the server.
MulticastSocket s;
try {
s = new MulticastSocket(port);
s.joinGroup(addr);
s.setLoopbackMode(true);
} catch (IOException e) {
throw new RuntimeException(e);
}
// send every 100 ms.
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0 ; i < 5; ++i) {
try {
System.out.println("Sending");
new MulticastSocket().send(sendPacket);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}).start();
// receive the packet.
DatagramPacket packet = new DatagramPacket(new byte[data.length], data.length);
try {
System.out.println("Listening");
s.receive(packet);
} catch (IOException e) {
s.close();
throw new RuntimeException(e);
}
s.close();
Assert.assertTrue(Arrays.equals(data, packet.getData()));
System.out.println("DATA: "+ new String(packet.getData()));
}
UPDATE: I forgot to include the output..
Listening Sending Sending Sending Sending Sending
At this point, it will just sit waiting to receive something.