Search code examples
javaandroidtcpscopeipv6

Migrating to Ipv6, how to impose the use of a specific scope?


I'm migrating my app from Ipv4 (TCP) to Ipv6 (TCP).

To contact the server, the client send a message to

fe80::cc3a:61ff:fe5d:bed5

instead of

192.168.0.16

The rest of the code remains identical.

Curiously, sometimes it works, sometimes it doesn't work. After a few hours of searching, I realized that a "scope" has been added to the end of the Ipv6 address (fe80::d6ae:5ff:fe43:c6e9%wlan0).

I noticed that it can be %p2p0, %eth0 or %wlan0. Knowing that to work through the Wi-Fi, the scope has to be %wlan0, how could I impose the use of a specific scope?

I looked all the Inet6Address methods without finding anything.


Solution

  • MulticastSocket.setNetworkInterface() is the solution.

    MulticastSocket multicastSocket = new MulticastSocket("5678");
    NetworkInterface wifiInterface = null;
    for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces()))
        if (networkInterface.getName().equalsIgnoreCase("wlan0")) {
            wifiInterface = networkInterface;
            break;
            }
    
    multicastSocket.setNetworkInterface(wifiInterface);