2003-01-10 Michael Koch <konqueror@gmx.de>
[platform/upstream/gcc.git] / libjava / java / net / ServerSocket.java
index 36f93e8..e2f8e63 100644 (file)
@@ -60,16 +60,11 @@ import java.nio.channels.ServerSocketChannel;
  */
 public class ServerSocket
 {
-
-  // Class Variables
-
   /**
    * This is the user defined SocketImplFactory, if one is supplied
    */
   private static SocketImplFactory factory;
 
-  // Instance Variables
-
   /**
    * This is the SocketImp object to which most instance methods in this
    * class are redirected
@@ -82,6 +77,8 @@ public class ServerSocket
    */
   private ServerSocketChannel ch;
 
+  private boolean closed = false;
+  
   /**
    * Constructor that simply sets the implementation.
    * 
@@ -95,6 +92,8 @@ public class ServerSocket
       impl = factory.createSocketImpl();
     else
       impl = new PlainSocketImpl();
+
+    impl.create(true);
   }
 
   /**
@@ -154,6 +153,7 @@ public class ServerSocket
     throws IOException
   {
     this();
+
     if (impl == null)
       throw new IOException("Cannot initialize Socket implementation");
 
@@ -184,19 +184,7 @@ public class ServerSocket
   public void bind (SocketAddress endpoint)
     throws IOException
   {
-    if (impl == null)
-      throw new IOException ("Cannot initialize Socket implementation");
-
-    if (! (endpoint instanceof InetSocketAddress))
-      throw new IllegalArgumentException ("Address type not supported");
-
-    InetSocketAddress tmp = (InetSocketAddress) endpoint;
-    
-    SecurityManager s = System.getSecurityManager ();
-    if (s != null)
-      s.checkListen (tmp.getPort ());
-
-    impl.bind (tmp.getAddress (), tmp.getPort ());
+    bind (endpoint, 50);
   }
  
   /**
@@ -214,6 +202,9 @@ public class ServerSocket
    */
   public void bind (SocketAddress endpoint, int backlog) throws IOException
   {
+    if (closed)
+      throw new SocketException ("ServerSocket is closed");
+    
     if (impl == null)
       throw new IOException ("Cannot initialize Socket implementation");
 
@@ -287,6 +278,13 @@ public class ServerSocket
    */
   public Socket accept () throws IOException
   {
+    if (impl == null)
+      throw new IOException ("Cannot initialize Socket implementation");
+
+    SecurityManager sm = System.getSecurityManager ();
+    if (sm != null)
+      sm.checkListen (impl.getLocalPort ());
+
     Socket s = new Socket();
     implAccept (s);
 
@@ -322,7 +320,13 @@ public class ServerSocket
    */
   public void close () throws IOException
   {
-    impl.close();
+    if (impl != null)
+      impl.close ();
+
+    if (ch != null)
+      ch.close ();
+    
+    closed = true;
   }
 
   /**
@@ -359,6 +363,16 @@ public class ServerSocket
   }
 
   /**
+   * Returns true if the socket is closed, otherwise false
+   * 
+   * @since 1.4
+   */
+  public boolean isClosed()
+  {
+    return closed;
+  }
+
+  /**
    * Sets the value of SO_TIMEOUT.  A value of 0 implies that SO_TIMEOUT is
    * disabled (ie, operations never time out).  This is the number of 
    * milliseconds a socket operation can block before an