0

From what I've read, it should be possible for two applications on the same host to be able to send and receive datagrams via multicast. I was trying to implement this, using the following Java code (which is a slightly modified version of what is given in the Javadoc for MulticastSocket):

    public static void main(String[] args) throws IOException{

        NetworkInterface nic = NetworkInterface.getByName("wlan4");

        int port = 6789;
        InetAddress group = InetAddress.getByName("228.5.6.7");
        MulticastSocket s = new MulticastSocket(port);
        s.setNetworkInterface(nic);
        s.joinGroup(group);

        if(args.length > 0 && args[0].equals("send")){
            System.out.println("SEND MODE");
            String msg = "Hello";
            DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(),
                                    group, port);
            s.send(hi);
        }else{

            byte[] buf = new byte[1000];
            DatagramPacket recv = new DatagramPacket(buf, buf.length);

            System.out.println("RECEIVE MODE");
            s.receive(recv);
            System.out.println(MessageFormat.format("Received: {0}",
                new String(recv.getData()).trim()));

            s.leaveGroup(group);
        }
    }

If I run the above code, giving send as input argument, the program executes just fine, it sends the packet and then terminates. However if I want to receive a packet, the program is blocked by the receive method as it never gets a datagram. I tested this by running multiple instances of the application on my machine, both with one and several receivers and one sender. Non gets any message at any time.

If I, on the other hand let the application receive what it just sent (by running the receive method unconditionally of wheter the application is sending), it works fine for that application alone. This triggers me to believe that the JVM instance has an exclusive bind on that socket, disallowing others to use it (even if the option getReuseAddress() returns true for MulticastSockets).

I'm running under Windows 10, and have verified that the UDP packet gets sent to the network using Wireshark, so I figured it has to do with that the packet is not delivered to the two applications.

What can I do in order to allow two applications to communicate over multicast on the same port number?

EDIT:

The overall idea is for a server to send a datagram to all listening clients on the network chosen (hence why the NIC is specified in the example as "wlan4"), irrespectively of where they are executed (e.g. on the same host as the server or not).

chrillof
  • 325
  • 2
  • 15
  • Why are you forcing the multicast socket to use `wlan4` when all the required communication is within the localhost? – user207421 Mar 09 '19 at 02:14
  • Good point, and I realize that I might not have explained my overall intentions. The idea is for a sender to send a datagram to be caught by all clients on a network, irrespectively of whether the clients reside on the same host as the server or not. – chrillof Mar 09 '19 at 13:21
  • Have you disabled multicast loopback? It seems to me that you are only receiving loopbacks, and that your `joinGroup()` hasn't taken effect outside your localhost at all. Try just removing the `setNetworkInterface()` line. – user207421 Mar 10 '19 at 23:40
  • Is there an explicit reason that the sender needs the source port of the packet to be the same as the destination port (6789 in your example)? If not, it would be much easier to have the sender send the packet from an arbitrary DatagramSocket, opened just for the purpose. Note that when sending a datagram, the sender doesn't care if the destination is a mcast or unicast address, i.e. you don't need a MulticastSocket for sending a mcast message. – dan.m was user2321368 Jun 23 '20 at 03:18

1 Answers1

0

After some debugging, I realized that I could receive multicast packets from several applications if the application itself were sending and receiving multicast datagrams. It turns out that sending a packet to the multicast group somehow triggers this functionality. It seems like a bug to me.

However, in order to get the above example work as expected, I had to send (and discard) a first datagram to the multicast channel. I did this in the most simple way, by changing the else block to the following:

        byte[] buf = new byte[1000];
        DatagramPacket recv = new DatagramPacket(buf, buf.length);
        s.send(new DatagramPacket("A".getBytes(), 1, group, port));
        s.receive(recv);

        System.out.println("RECEIVE MODE");
        s.receive(recv);
        System.out.println(MessageFormat.format("Received: {0}",
                new String(recv.getData()).trim()));

        s.leaveGroup(group);
chrillof
  • 325
  • 2
  • 15