JDK-4417033 : MulticastSocket.joinGroup() failes with setInterface() to loopback under Linux
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2001-02-20
  • Updated: 2001-02-26
  • Resolved: 2001-02-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.0 betaFixed
Related Reports
Relates :  
Relates :  
Description

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) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: generic FIXED IN: merlin-beta INTEGRATED IN: merlin-beta VERIFIED IN: merlin-beta
14-06-2004

WORK AROUND Name: boT120536 Date: 02/20/2001 A route statement can be added forcing the kernel to listen for multicast packets on the loopback interface, but this breaks all multicast applications trying to use any other interface. Route statement follows: route add -net 224.0.0.0 netmask 240.0.0.0 dev lo ======================================================================
11-06-2004

EVALUATION The assessment in the Description part is exactly correct. And we have added the appropriate APIs in MulticastSocket to address the issue. public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) throws IOException Joins the specified multicast group at the specified interface. leaveGroup public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) throws IOException Leave a multicast group on a specified local interface. yingxian.wang@eng 2001-02-26
26-02-2001