Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4417033

MulticastSocket.joinGroup() failes with setInterface() to loopback under Linux

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • core-libs
    • beta
    • x86
    • linux
    • Verified



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

            ywangsunw Yingxian Wang (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: