java.net
Class ServerSocket

java.lang.Object
  |
  +--java.net.ServerSocket

public final class ServerSocket
extends Object

The server Socket class. It uses a SocketImpl to implement the actual socket operations. It is done this way so that you are able to change socket implementations depending on the kind of firewall being used. You can change socket implementations by setting the SocketImplFactory.


Constructor Summary
ServerSocket(int port)
          Creates a server socket on a specified port.
ServerSocket(int port, int backlog)
          Creates a server socket, binds it to the specified local port and listens to it.
 
Method Summary
 Socket accept()
          Accepts a connection.
 void close()
          Closes the server socket.
 InetAddress getInetAddress()
          Gets the address to which the socket is connected.
 int getLocalPort()
          Gets the port on which the socket is listening.
static void setSocketFactory(SocketImplFactory fac)
          Sets the system's server SocketImplFactory.
 String toString()
          Returns the implementation address and implementation port of this ServerSocket as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ServerSocket

public ServerSocket(int port)
             throws IOException
Creates a server socket on a specified port.

Parameters:
port - the port
Throws:
IOException - IO error when opening the socket.

ServerSocket

public ServerSocket(int port,
                    int backlog)
             throws IOException
Creates a server socket, binds it to the specified local port and listens to it. You can connect to an annonymous port by specifying the port number to be 0. backlog<\i> specifies how many connection requests the system will queue up while waiting for the ServerSocket to execute accept().

Parameters:
port - the specified port
backlog - the number of queued connect requests pending accept
Method Detail

getInetAddress

public InetAddress getInetAddress()
Gets the address to which the socket is connected.


getLocalPort

public int getLocalPort()
Gets the port on which the socket is listening.


accept

public Socket accept()
              throws IOException
Accepts a connection. This method will block until the connection is made.

Throws:
IOException - IO error when waiting for the connection.

close

public void close()
           throws IOException
Closes the server socket.

Throws:
IOException - IO error when closing the socket.

toString

public String toString()
Returns the implementation address and implementation port of this ServerSocket as a String.

Overrides:
toString in class Object

setSocketFactory

public static void setSocketFactory(SocketImplFactory fac)
                             throws IOException
Sets the system's server SocketImplFactory. The factory can be specified only once.

Parameters:
fac - the desired factory
Throws:
SocketException - If the factory has already been defined.
IOException - IO error when setting the socket factor.