1

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.

vangorra
  • 1,631
  • 19
  • 24

3 Answers3

0

A simple thing could be if you are behind a router. Your router might be receiving the incoming request but doesn't know where to send it to. This would require you to forward port 5555 to the proper internal IP, the machine that is running your server program.

One way you could check against this would be to run both the client and server locally using the IP 127.0.0.1 or localhost.

Kevin Sheehan
  • 408
  • 3
  • 7
  • MulticastSocket won't support 127.0.0.1 as it is not a multicast address. I have tested this on 3 separate networks that I confirmed worked in the past. – vangorra Mar 20 '14 at 20:19
  • Alright, but do you think that the problem could be the port forwarding issue? If you aren't receiving packets at all that could be very likely. – Kevin Sheehan Mar 20 '14 at 20:34
  • 1
    So I explicitly called MulticastSocket.joinGroup for each defined network interface. And that works! – vangorra Mar 20 '14 at 20:44
0

You may be hitting a race condition between the sending thread and the main execution. If all of the sends complete prior to the main process blocking on the receive, you will wait forever. Try putting a minor sleep in the thread before you start the send ping (something like 100ms) and see if you begin to see anything. Also check any firewall settings, that's definitely burned me in the past.

SeaNick
  • 501
  • 1
  • 4
  • 14
  • I forgot to add the output of the example I provided. It does a good job showing that the race condition is accounted for. Checked my firewall for the nth time. Still off. – vangorra Mar 20 '14 at 20:17
0

One other possibility (forgive my uncertainty) is the shared memory usage for the socket object. Is there anything stopping you from having separate socket objects, one for send and one for receive?

SeaNick
  • 501
  • 1
  • 4
  • 14