Uploaded image for project: 'JGroups'
  1. JGroups
  2. JGRP-777

Revisit multicast socket creation code

XMLWordPrintable

    • Icon: Task Task
    • Resolution: Done
    • Icon: Major Major
    • 2.4.5, 2.6.5, 2.7
    • None
    • None

      [Dieter Cailliau]

      I looked into the UDP class (trunk) and found out that the MulticastSocket(port) constructor is used.
      This doesn't work well on many linux systems: http://forum.java.sun.com/thread.jspa?threadID=5216130&messageID=9875372 It's not entirely clear who's 'fault' this is, but i have a workaround below.

      I experienced this problem in jgroups when i tried to set up 2 distinct processes on the same machine, that use a different stack with different UDP mcast_addr. Still the one process would complain about the other: discard msg from different group.
      java 1.5.0_10-b03, jgroups 2.5.0.GA.

      The code i use for making MulticastSockets is below. In combination with a default gateway and preferIPv4 this was the only way to make sure that my socket only received datagrams for the group it actually joined. Using the constructor with only a port results in a sokcet receiving ALL datagrams that are multicasted to that port (regardless of the group address it joined!).

      Can someone please give some feedback on this, and consider if this is a fix for UDP?

      public static MulticastSocket createMulticastSocket(int TIMEOUT_MS, InetAddress group, int port) {
      MulticastSocket socket = null;
      try

      { socket = new MulticastSocket(new InetSocketAddress(group, port)); }

      catch (BindException e1) {
      String warn = null == System.getProperty("java.net.preferIPv4Stack") ? "-Djava.net.preferIPv4Stack != true ??" : "java.net.preferIPv4Stack=true";
      log.warn("Unable to create the multicast socket at address " + group + ":" + port + "; I'll fall back to the windows solution (create the socket with port only) " + warn,e1);
      // Windows fallback
      try

      { socket = new MulticastSocket(new InetSocketAddress(port)); }

      catch (IOException e)

      { throw new RuntimeException(e1); }

      } catch (Exception e1)

      { throw new RuntimeException(e1); }

      try

      { socket.setSoTimeout(TIMEOUT_MS); socket.joinGroup(group); }

      catch (Exception e1) {
      byte[] address = group.getAddress();
      String ad = address[0] + "." + address[1] + "." + address[2] + "." + address[3] + ":" + port + " " + e1.toString();
      try

      { socket.close(); }

      catch(Exception c)

      { log.warn("Ignore exception while closing bad multicast socket: " + c); }

      throw new RuntimeException(ad,e1);
      }
      return socket;
      }

              rhn-support-jawilson Jimmy Wilson
              rhn-engineering-bban Bela Ban
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: