    All Packages  Class Hierarchy  This Package  Previous  Next  Index

# Class java.net.ServerSocket

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

public 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.

##

ServerSocket(int)
  Creates a server socket on a specified port.
ServerSocket(int, int)
  Creates a server socket, binds it to the specified local port and listens to it.
ServerSocket(int, int, InetAddress)
  Create a server with the specified port, listen backlog, and local IP address to bind to.

##

accept()
  Accepts a connection.
close()
  Closes the server socket.
getInetAddress()
  Gets the local address of this ServerSocket.
getLocalPort()
  Gets the port on which the socket is listening.
getSoTimeout()
  Retrive setting for SO_TIMEOUT.
implAccept(Socket)
  Subclasses of ServerSocket use this method to override accept() to return their own subclass of socket.
setSocketFactory(SocketImplFactory)
  Sets the system's server SocketImplFactory.
setSoTimeout(int)
  Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
toString()
  Returns the implementation address and implementation port of this ServerSocket as a String.

##

ServerSocket

      public ServerSocket(int port) throws IOException

  Creates a server socket on a specified port. The port must be between 0 and 65535, inclusive.

  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 specifies how many connection requests the system will queue up while waiting for the ServerSocket to execute accept(). The port must be between 0 and 65535, inclusive.

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

ServerSocket

      public ServerSocket(int port,
                          int backlog,
                          InetAddress bindAddr) throws IOException

  Create a server with the specified port, listen backlog, and local IP address to bind to. The bindAddr argument can be used on a multi-homed host for a ServerSocket that will only accept connect requests to one of its addresses. If bindAddr is null, it will default accepting connections on any/all local addresses. The port must be between 0 and 65535, inclusive.

  Parameters:
    port - the local TCP port
    backlog - the listen backlog
    bindAddr - the local InetAddress the server will bind to
  See Also:
    SocketConstants, SocketOption, SocketImpl

##

getInetAddress

      public InetAddress getInetAddress()

  Gets the local address of this ServerSocket.

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.

implAccept

      protected final void implAccept(Socket s) throws IOException

  Subclasses of ServerSocket use this method to override accept() to return their own subclass of socket. So a FooServerSocket will typically hand this method an empty FooSocket(). On return from implAccept the FooSocket will be connected to a client.

close

      public void close() throws IOException

  Closes the server socket.

  Throws: IOException
    IO error when closing the socket.

setSoTimeout

      public synchronized void setSoTimeout(int timeout) throws SocketException

  Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to accept() for this ServerSocket will block for only this amount of time. If the timeout expires, a java.io.InterruptedIOException is raised, though the ServerSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

getSoTimeout

      public synchronized int getSoTimeout() throws IOException

  Retrive setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).

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 synchronized 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.
  Throws: IOException
    IO error when setting the socket factor.

    All Packages  Class Hierarchy  This Package  Previous  Next  Index