After updating the Java version from 12 to 15, this code now throws a rather non-descriptive SocketException
on Windows, but only if IPv6 is disabled on the NetworkInterface
:
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
DatagramSocket socket = new MulticastSocket(65533);
((MulticastSocket) socket).setTimeToLive(128);
((MulticastSocket) socket).setNetworkInterface(networkInterface); // <-- Exception thrown here
This is the exception:
java.net.SocketException: Invalid argument: no further information
at java.base/sun.nio.ch.Net.setInterface6(Native Method)
at java.base/sun.nio.ch.DatagramChannelImpl.setOption(DatagramChannelImpl.java:364)
at java.base/sun.nio.ch.DatagramSocketAdaptor.setOption(DatagramSocketAdaptor.java:418)
at java.base/sun.nio.ch.DatagramSocketAdaptor.setNetworkInterface(DatagramSocketAdaptor.java:600)
at java.base/java.net.MulticastSocket.setNetworkInterface(MulticastSocket.java:466)
...
However, if I create the Socket
like this, no exception is thrown:
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
DatagramSocket socket = new MulticastSocket(
new InetSocketAddress(Collections.list(networkInterface.getInetAddresses()).get(0), 65533));
((MulticastSocket) socket).setTimeToLive(128);
((MulticastSocket) socket).setNetworkInterface(networkInterface); // <-- No exception is thrown
Is this expected/intended behaviour? If so, is this documented somewhere?
This test script...
System.out.println("Example 1");
try {
NetworkInterface netIf1 = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
System.out.println(netIf1 + " " + Collections.list(netIf1.getInetAddresses()).get(0));
DatagramSocket sock1 = new MulticastSocket(65533);
((MulticastSocket) sock1).setTimeToLive(128);
((MulticastSocket) sock1).setNetworkInterface(netIf1);
System.out.println("Success");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Example 2");
try {
NetworkInterface netIf2 = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
System.out.println(netIf2 + " " + Collections.list(netIf2.getInetAddresses()).get(0));
DatagramSocket sock2 = new MulticastSocket(new InetSocketAddress(Collections.list(netIf2.getInetAddresses()).get(0), 65533));
((MulticastSocket) sock2).setTimeToLive(128);
((MulticastSocket) sock2).setNetworkInterface(netIf2);
System.out.println("Success");
} catch (Exception e) {
e.printStackTrace();
}
...prints the following statements to the console:
Example 1
name:eth8 (Intel(R) Ethernet Connection (7) I219-LM) /10.240.10.193
java.net.SocketException: Invalid argument: no further information
at java.base/sun.nio.ch.Net.setInterface6(Native Method)
at java.base/sun.nio.ch.DatagramChannelImpl.setOption(DatagramChannelImpl.java:364)
at java.base/sun.nio.ch.DatagramSocketAdaptor.setOption(DatagramSocketAdaptor.java:418)
at java.base/sun.nio.ch.DatagramSocketAdaptor.setNetworkInterface(DatagramSocketAdaptor.java:600)
at java.base/java.net.MulticastSocket.setNetworkInterface(MulticastSocket.java:466)
at com.example.MyApplication.createSocket(MyApplication.java:32)
Example 2
name:eth8 (Intel(R) Ethernet Connection (7) I219-LM) /10.240.10.193
Success
join()
s will be sent. The second determines the IP address to which the socket is bound, i.e. the IP address via which it can receive. You need to show what the actualNetworkInterface
obtained by your first piece of code is.MulticastSocket
constructor with theInetSocketAddress
with the interface IP as an argument. The network interface which is used in this example is my main ethernet adapter (the build-in Intel network chipset)NetworkInterface
in both cases.new MulticastSocket(65533)
this creates a multicast socket bound to the wildcard address. Because your OS/machine supports IPv6 the underlying socket will be IPv6. When you then try to set the NetworkInterface to an interface on which IPv6 is disabled, it fails. The difference in behavior compared to Java 12 is probably an unintended side effect of JEP 373 openjdk.java.net/jeps/373