Name: boT120536 Date: 02/20/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)
Multicast receiving does not work under Linux when the listening Multicast
socket is bound to the loopback interface with MulticastSocket.setInterface().
This can be demonstrated with the code below. If run with the loopback interface
specified by passing in a command-line argument of 127.0.0.1, with a packet
sniffer, such as tcpdump, you can see one packet go out, but not come back in.
This could be, as suggested by Alexey Kuznet from the linux-kernel mailing list:
I think this java forgets to call IP_ADD_MEMBERSHIP or calls
it with invalid arguments. I even suspect what is wrong, it uses
"unspecified" interface. When interface is unspecified, it falls
to default, which is not loopback of course.
// Code snippet below--------------------------------------------
import java.net.*;
/**
* Test for multicast functionality accross specified interfaces.
* To show bug, execute:
* java MulticastPing <your LAN address>
* then:
* java MulticastPing 127.0.0.1
*/
public class MulticastPing extends Thread {
int multicastPort = 6000;
String multicastAddress = "224.1.0.1";
String interfaceAddress;
public static void main(String[] argv) {
MulticastPing mc = new MulticastPing(argv[0]);
mc.listen();
mc.send();
}
MulticastPing(String interfaceAddress) {
this.interfaceAddress = interfaceAddress;
}
void listen() {
this.start();
}
public void run() {
try {
MulticastSocket sock = new MulticastSocket(multicastPort);
sock.setInterface(InetAddress.getByName(interfaceAddress));
sock.joinGroup(InetAddress.getByName(multicastAddress));
System.out.println("Listening for packets.");
DatagramPacket buf = new DatagramPacket(new byte[1024], 1024);
sock.receive(buf);
System.out.println("Got: " + new String(buf.getData()));
} catch (Exception e) {
System.err.println("Receive Exception: " + e);
e.printStackTrace();
}
}
void send() {
try {
sleep(1000); // Wait for receive thread to settle
MulticastSocket sock = new MulticastSocket(multicastPort);
sock.setInterface(InetAddress.getByName(interfaceAddress));
sock.joinGroup(InetAddress.getByName(multicastAddress));
byte[] data = new String("Himom").getBytes();
DatagramPacket buf = new DatagramPacket(data, data.length,
InetAddress.getByName(multicastAddress), multicastPort);
sock.send(buf);
System.out.println("Packet sent.");
} catch (Exception e) {
System.err.println("Send Exception: " + e);
e.printStackTrace();
}
}
}
// Code snippet ends ----------------------------------------------------
(Review ID: 117070)
======================================================================
- relates to
-
JDK-6402758 Linux: Problem with multicast socket on dual stack (ipv4 and ipv6) on the loopback interface
- Closed
-
JDK-6393257 MulticastSocket.setInterface() has no effect on linux when interface is loopback
- Closed