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

Win32: java.net.Socket cannot be bound to 0.0.0.0

XMLWordPrintable

    • merlin
    • sparc
    • solaris_2.5



      Name: akR10050 Date: 06/05/2000



      java.net.Socket class constructors throw java.net.BindException on win32
      platforms when trying to bind to 0.0.0.0 interface, which stands for all
      interfaces at the given host (INADDR_ANY). This is true for all constructors
      which accepts InetAddress (or hostname string) as the argument.

      For example, BindException is thrown when trying to create Socket on the same
      interface and port as ServerSocket using its getter methods.

      ------------------ Test.java --------------------------

      import java.net.*;
      import java.io.IOException;

      public class Test {
          public static void main(String[] args) {
              ServerSocket srvSock = null;
              Socket sock = null;
              // create ServerSocket
              try {
                  srvSock = new ServerSocket(0);
              } catch (IOException ioe) {
                  System.out.println("FAILED: IOException while creating socket!");
              }
              System.out.println("InetAddress of the ServerSocket instance is "
                                 + srvSock.getInetAddress());
              // create Socket on the same interface and port as ServerSocket was
              // created
              try {
                  sock = new Socket(srvSock.getInetAddress(),
                                    srvSock.getLocalPort());
              } catch (BindException be) {
                  System.out.println("FAILED: BindException while creating Socket");
                  return;
              } catch (IOException ioe) {
                  System.out.println("FAILED: Unexpected IOException");
                  return;
              } catch (SecurityException se) {
                  System.out.println("FAILED: SecurityException occured");
                  return;
              }
              System.out.println("PASSED: Socket created");
          }
      }

      -------------------------------------------------------

      Output under Windows NT (Service Pack 6)
      ---------------- Output -------------------------------

      D:\>java -version
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      D:\>java Test
      InetAddress of the ServerSocket instance is 0.0.0.0/0.0.0.0
      FAILED: BindException while creating Socket
      -------------------------------------------------------

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

            ywangsunw Yingxian Wang (Inactive)
            kaisunw Kai Kai (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: