3

I have the following issue:

I want to send multicastd data to f.i. 224.0.12.13, but as the user's machine maybe have a lot of devices, the data is sent (obviously) to the route with the smallest metric. For instance, "route print" in windows tells me:

    ===========================================================================
Schnittstellenliste
 38...00 15 5d 25 b1 ec ......Hyper-V Virtual Ethernet Adapter
 33...3e 37 86 8b cd 0d ......Microsoft Wi-Fi Direct Virtual Adapter #4
 47...3c 37 86 8b cd 0d ......Microsoft Wi-Fi Direct Virtual Adapter #5
  7...3c 37 86 8b cd 0d ......NETGEAR A6150 WiFi USB 2.0 Adapter
  1...........................Software Loopback Interface 1
===========================================================================

IPv4-Routentabelle
===========================================================================
Aktive Routen:
     Netzwerkziel    Netzwerkmaske          Gateway    Schnittstelle Metrik
          0.0.0.0          0.0.0.0     192.168.10.1    192.168.10.49     35
        127.0.0.0        255.0.0.0   Auf Verbindung         127.0.0.1    331
        127.0.0.1  255.255.255.255   Auf Verbindung         127.0.0.1    331
  127.255.255.255  255.255.255.255   Auf Verbindung         127.0.0.1    331
      172.18.64.0    255.255.240.0   Auf Verbindung       172.18.64.1    271
      172.18.64.1  255.255.255.255   Auf Verbindung       172.18.64.1    271
    172.18.79.255  255.255.255.255   Auf Verbindung       172.18.64.1    271
     192.168.10.0    255.255.255.0   Auf Verbindung     192.168.10.49    291
    192.168.10.49  255.255.255.255   Auf Verbindung     192.168.10.49    291
   192.168.10.255  255.255.255.255   Auf Verbindung     192.168.10.49    291
        224.0.0.0        240.0.0.0   Auf Verbindung         127.0.0.1    331
        224.0.0.0        240.0.0.0   Auf Verbindung     192.168.10.49    291
        224.0.0.0        240.0.0.0   Auf Verbindung       172.18.64.1    271
  255.255.255.255  255.255.255.255   Auf Verbindung         127.0.0.1    331
  255.255.255.255  255.255.255.255   Auf Verbindung     192.168.10.49    291
  255.255.255.255  255.255.255.255   Auf Verbindung       172.18.64.1    271
===========================================================================

As you can see, there are three 224.0.0.0 routes. Java typically picks the one in the 172.16.64.1 net/device and not the desired 192.168.10.49

In my code I know the device's name and IP that should be used to send multicast data.

But how can I overcome the operating system to pick the one with the smallest metric?

Changing the metric is not an option. It must be possible to tell java which interface to use when sending multicast data and not choose with help of routing table.

Can anyone help?

My code snippet to test sending and watching which interface is taken with help of wireshark:

        int port = 5000;
        String group = "224.0.23.13";
        
        final MulticastSocket s = new MulticastSocket();
        byte[] buf = new byte[10];
        for (int i = 0; i < buf.length; i++) {
            buf[i] = (byte) i;
        }
        
        final DatagramPacket pack = new DatagramPacket(buf, buf.length, InetAddress.getByName(group), port);
        s.send(pack);
Alex
  • 365
  • 1
  • 2
  • 9
  • I believe, but I am not 100% sure, that you should use the `MulticastSocket` constructor accepting a `SocketAddress`, passing an instance of `SocketAddress` constructed with the IP address of the interface you want to use . – Mark Rotteveel Oct 07 '21 at 17:51
  • Did some experiments.. Seems like ```java MulticastSocket s = new MulticastSocket(new InetSocketAddress("192.168.10.49", 3671)); ``` does the trick. verified with wireshark. Thanks. – Alex Oct 08 '21 at 13:35
  • Good to hear! I have posted a variant of my comment as an answer. – Mark Rotteveel Oct 08 '21 at 13:39

2 Answers2

2

The proper way to do this is to use the setNetworkInterface method, which sets the outgoing network interface for multicast datagrams.

MulticastSocket s = new MulticastSocket();
s.setNetworkInterface(NetworkInterface.getByName("192.168.10.49"));
dbush
  • 205,898
  • 23
  • 218
  • 273
1

As far as I know, you should use the MulticastSocket constructor accepting a SocketAddress, passing an instance of SocketAddress constructed with the IP address of the interface you want to use.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197