Search code examples
javanetwork-programmingmulticastmulticastsocket

Multicast between applications on the same host


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).


Solution

  • 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);