Hi,

Committed. This fixes several bugs in ServerSocket.bind().

Regards,
Jeroen

2006-09-24  Jeroen Frijters  <[EMAIL PROTECTED]>

        * java/net/ServerSocket.java
        (bind(SocketAddress,int)): Added support for null address.
        Throw proper exception if already bound.
        Handle unresolved addresses correctly. Ignore exceptions that
        happen during close in error path (to prevent losing the
original
        exception.)
Index: java/net/ServerSocket.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/net/ServerSocket.java,v
retrieving revision 1.47
diff -u -r1.47 ServerSocket.java
--- java/net/ServerSocket.java  19 Sep 2006 05:47:39 -0000      1.47
+++ java/net/ServerSocket.java  24 Sep 2006 15:36:58 -0000
@@ -221,44 +221,53 @@
     if (isClosed())
       throw new SocketException("ServerSocket is closed");
 
-    if (! (endpoint instanceof InetSocketAddress))
-      throw new IllegalArgumentException("Address type not supported");
+    if (isBound())
+      throw new SocketException("Already bound");
 
-    InetSocketAddress tmp = (InetSocketAddress) endpoint;
+    InetAddress addr;
+    int port;
+
+    if (endpoint == null)
+      {
+        addr = InetAddress.ANY_IF;
+        port = 0;
+      }
+    else if (! (endpoint instanceof InetSocketAddress))
+      {
+        throw new IllegalArgumentException("Address type not supported");
+      }
+    else
+      {
+        InetSocketAddress tmp = (InetSocketAddress) endpoint;
+        if (tmp.isUnresolved())
+          throw new SocketException("Unresolved address");
+        addr = tmp.getAddress();
+        port = tmp.getPort();
+      }
 
     SecurityManager s = System.getSecurityManager();
     if (s != null)
-      s.checkListen(tmp.getPort());
-
-    InetAddress addr = tmp.getAddress();
-
-    // Initialize addr with 0.0.0.0.
-    if (addr == null)
-      addr = InetAddress.ANY_IF;
+      s.checkListen(port);
 
     try
       {
-        port = tmp.getPort();
        impl.bind(addr, port);
        impl.listen(backlog);
-       local = new InetSocketAddress(
+        this.port = port;
+        local = new InetSocketAddress(
             (InetAddress) impl.getOption(SocketOptions.SO_BINDADDR),
             impl.getLocalPort());
       }
-    catch (IOException exception)
-      {
-       close();
-       throw exception;
-      }
-    catch (RuntimeException exception)
-      {
-       close();
-       throw exception;
-      }
-    catch (Error error)
+    finally
       {
-       close();
-       throw error;
+        try
+          {
+            if (local == null)
+             close();
+          }
+        catch (IOException _)
+          {
+          }
       }
   }
 

Reply via email to