*/
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
*/
private ServerSocketChannel ch;
+ private boolean closed = false;
+
/**
* Constructor that simply sets the implementation.
*
impl = factory.createSocketImpl();
else
impl = new PlainSocketImpl();
+
+ impl.create(true);
}
/**
throws IOException
{
this();
+
if (impl == null)
throw new IOException("Cannot initialize Socket implementation");
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);
}
/**
*/
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");
*/
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);
*/
public void close () throws IOException
{
- impl.close();
+ if (impl != null)
+ impl.close ();
+
+ if (ch != null)
+ ch.close ();
+
+ closed = true;
}
/**
}
/**
+ * 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