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

MulticastSocket.setReuseAddress(true) is ignored on windows platforms

XMLWordPrintable

    • x86
    • windows_95



      Name: dfR10049 Date: 06/29/2001


      Javadoc states about java.net.DatagramSocket.setReuseAddress(boolean on):

          Enable/disable SO_REUSEADDR. The SO_REUSEADDR socket option affects the
          bind() operation and serves 2 main different purposes:
            o Allows completely duplicate bindings (same address and port) on multicast
              sockets, in which case when a datagram is received for one of these multiply
              bound sockets, one copy of the the datagram is delivered to each matching socket.
            o Allows a single process to bind the same port to multiple sockets as long as each
              bind specifies a different local IP address.

          Note that setReuseAddress(true) will only be enabled on the socket if the underlying
          network implementation can guarantee that this can be done safely. Thus enabling
          SO_REUSEADDR should be considered as a hint and applications that wish to verify
          wether SO_REUSEADDR has been enabled should call getReuseAddress().

      So if getReuseAddress() returns true for MulticastSocket then bind() operation
      should be allowed even if port is already in use. This works on solaris,
      but does not work on win98 and win2000

      This is the test demonstrating the bug:
      ------------------------------------------------------------------
      import java.net.*;

      public class DSTest {

          public static void main (String args[]) {


              int port = Integer.decode(args[0]).intValue();

              MulticastSocket ms1 = null;
              MulticastSocket ms2 = null;
              try {
          
                  InetAddress localAddr = InetAddress.getLocalHost();
                  InetSocketAddress addr = new InetSocketAddress(localAddr, port);
             
                  ms1 = new MulticastSocket(null);
                  try {
                      ms1.bind(addr);
                  } catch (SocketException e) {
                      System.out.println("cannot bind first socket to " + addr
                              + " unexpected " + e);
                      return;
                  }
               
                  ms2 = new MulticastSocket(null);
                  ms2.setReuseAddress(true);
                  if (!ms2.getReuseAddress()) {
                      System.out.println("Cannot check: "
                          + " safety for SO_REUSEADDR option is not guaranteed");
                      return;
                  }
              
                  System.out.println("getReuseAddress(): " + ms2.getReuseAddress());
                  try {
                      ms2.bind(addr);
                  } catch (SocketException e) {
                      System.out.println("cannot bind second socket to " + addr
                              + " unexpected " + e);
                      return;
                  }
              
                  System.out.println("Test passed");
              } catch (Exception e) {
                  System.out.println("Unexpected: " + e);
                  e.printStackTrace(System.out);
              }

          }
          
      }

      ---------------------------------------------------------------------
      output from the test:

      on Solaris:
      #> java DSTest 12345
      getReuseAddress(): true
      Test passed

      on win98 and win2000:
      #> java DSTest 12345
      getReuseAddress(): true
      cannot bind second socket to vento/192.168.205.171:12345 unexpected java.net.BindException: Address already in use: Cannot bind

      ======================================================================

            Unassigned Unassigned
            fdasunw Fda Fda (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: