When MulticastSocket.setInterface() is called with loopack interface, the address that is set to the outgoing packet is still the default interface. See the following program.
import java.io.*;
import java.net.*;
public class MulticastLoopbackTest {
public static void main(String[] args) throws Exception {
MulticastSocket sender = new MulticastSocket();
MulticastSocket receiver = new MulticastSocket(3000);
InetAddress multicast = InetAddress.getByName("224.80.80.80");
SocketAddress sa = new InetSocketAddress(multicast, 0);
InetAddress localhost = InetAddress.getByName("127.0.0.1");
receiver.joinGroup(sa, NetworkInterface.getByInetAddress(localhost));
byte[] testbytes = {1, 2};
DatagramPacket packet = new DatagramPacket(testbytes, testbytes.length, multicast, 3000);
sender.joinGroup(sa, NetworkInterface.getByInetAddress(localhost));
sender.setInterface(localhost);
sender.send(packet);
byte[] buffer = new byte[10];
DatagramPacket p = new DatagramPacket(buffer, 10);
receiver.receive(p);
System.out.println(p.getAddress());
}
}
The output from JDK 5.0 on linux is the actual IP address of the host. When the network card is turned off, it prints out the wildcard address 0.0.0.0. The expected output is 127.0.0.1.
Please note that to run the above program on linux, the workaround for 4417033 has to be applied to change the routing table.
The problem is not reproducible on Solaris.
import java.io.*;
import java.net.*;
public class MulticastLoopbackTest {
public static void main(String[] args) throws Exception {
MulticastSocket sender = new MulticastSocket();
MulticastSocket receiver = new MulticastSocket(3000);
InetAddress multicast = InetAddress.getByName("224.80.80.80");
SocketAddress sa = new InetSocketAddress(multicast, 0);
InetAddress localhost = InetAddress.getByName("127.0.0.1");
receiver.joinGroup(sa, NetworkInterface.getByInetAddress(localhost));
byte[] testbytes = {1, 2};
DatagramPacket packet = new DatagramPacket(testbytes, testbytes.length, multicast, 3000);
sender.joinGroup(sa, NetworkInterface.getByInetAddress(localhost));
sender.setInterface(localhost);
sender.send(packet);
byte[] buffer = new byte[10];
DatagramPacket p = new DatagramPacket(buffer, 10);
receiver.receive(p);
System.out.println(p.getAddress());
}
}
The output from JDK 5.0 on linux is the actual IP address of the host. When the network card is turned off, it prints out the wildcard address 0.0.0.0. The expected output is 127.0.0.1.
Please note that to run the above program on linux, the workaround for 4417033 has to be applied to change the routing table.
The problem is not reproducible on Solaris.
- duplicates
-
JDK-4742177 Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
- Closed
- relates to
-
JDK-4701650 MulticastSocket spec issues
- Open
-
JDK-4417033 MulticastSocket.joinGroup() failes with setInterface() to loopback under Linux
- Closed