Package java.net

Class ServerSocket

java.lang.Object
java.net.ServerSocket
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
SSLServerSocket

public class ServerSocket
extends Object
implements Closeable
This class represents a server-side socket that waits for incoming client connections. A ServerSocket handles the requests and sends back an appropriate reply. The actual tasks that a server socket must accomplish are implemented by an internal SocketImpl instance.
  • Constructor Details

    • ServerSocket

      public ServerSocket() throws IOException
      Constructs a new unbound ServerSocket.
      Throws:
      IOException - if an error occurs while creating the socket.
    • ServerSocket

      public ServerSocket​(int port) throws IOException
      Constructs a new ServerSocket instance bound to the given port. The backlog is set to 50. If port == 0, a port will be assigned by the OS.
      Throws:
      IOException - if an error occurs while creating the socket.
    • ServerSocket

      public ServerSocket​(int port, int backlog) throws IOException
      Constructs a new ServerSocket instance bound to the given port. The backlog is set to backlog. If port == 0, a port will be assigned by the OS.
      Throws:
      IOException - if an error occurs while creating the socket.
    • ServerSocket

      public ServerSocket​(int port, int backlog, InetAddress localAddress) throws IOException
      Constructs a new ServerSocket instance bound to the given localAddress and port. The backlog is set to backlog. If localAddress == null, the ANY address is used. If port == 0, a port will be assigned by the OS.
      Throws:
      IOException - if an error occurs while creating the socket.
  • Method Details

    • getImpl$

      public SocketImpl getImpl$()
    • accept

      public Socket accept() throws IOException
      Waits for an incoming request and blocks until the connection is opened. This method returns a socket object representing the just opened connection.
      Returns:
      the connection representing socket.
      Throws:
      IOException - if an error occurs while accepting a new connection.
    • close

      public void close() throws IOException
      Closes this server socket and its implementation. Any attempt to connect to this socket thereafter will fail.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if an error occurs while closing this socket.
    • getInetAddress

      public InetAddress getInetAddress()
      Gets the local IP address of this server socket or null if the socket is unbound. This is useful for multihomed hosts.
      Returns:
      the local address of this server socket.
    • getLocalPort

      public int getLocalPort()
      Gets the local port of this server socket or -1 if the socket is unbound.
      Returns:
      the local port this server is listening on.
    • getSoTimeout

      public int getSoTimeout() throws IOException
      Gets the socket accept timeout.
      Throws:
      IOException - if the option cannot be retrieved.
    • implAccept

      protected final void implAccept​(Socket aSocket) throws IOException
      Invokes the server socket implementation to accept a connection on the given socket aSocket.
      Parameters:
      aSocket - the concrete SocketImpl to accept the connection request on.
      Throws:
      IOException - if the connection cannot be accepted.
    • setSocketFactory

      public static void setSocketFactory​(SocketImplFactory aFactory) throws IOException
      Sets the server socket implementation factory of this instance. This method may only be invoked with sufficient security privilege and only once during the application lifetime.
      Parameters:
      aFactory - the streaming socket factory to be used for further socket instantiations.
      Throws:
      IOException - if the factory could not be set or is already set.
    • setSoTimeout

      public void setSoTimeout​(int timeout) throws SocketException
      Sets the accept timeout in milliseconds for this socket. This accept timeout defines the period the socket will block waiting to accept a connection before throwing an InterruptedIOException. The value 0 (default) is used to set an infinite timeout. To have effect this option must be set before the blocking method was called.
      Parameters:
      timeout - the timeout in milliseconds or 0 for no timeout.
      Throws:
      SocketException - if an error occurs while setting the option.
    • toString

      public String toString()
      Returns a textual representation of this server socket including the address, port and the state. The port field is set to 0 if there is no connection to the server socket.
      Overrides:
      toString in class Object
      Returns:
      the textual socket representation.
    • bind

      public void bind​(SocketAddress localAddr) throws IOException
      Binds this server socket to the given local socket address with a maximum backlog of 50 unaccepted connections. If the localAddr is set to null the socket will be bound to an available local address on any free port of the system.
      Parameters:
      localAddr - the local address and port to bind on.
      Throws:
      IllegalArgumentException - if the SocketAddress is not supported.
      IOException - if the socket is already bound or a problem occurs during binding.
    • bind

      public void bind​(SocketAddress localAddr, int backlog) throws IOException
      Binds this server socket to the given local socket address. If the localAddr is set to null the socket will be bound to an available local address on any free port of the system.
      Parameters:
      localAddr - the local machine address and port to bind on.
      backlog - the maximum number of unaccepted connections. Passing 0 or a negative value yields the default backlog of 50.
      Throws:
      IllegalArgumentException - if the SocketAddress is not supported.
      IOException - if the socket is already bound or a problem occurs during binding.
    • getLocalSocketAddress

      public SocketAddress getLocalSocketAddress()
      Gets the local socket address of this server socket or null if the socket is unbound. This is useful on multihomed hosts.
      Returns:
      the local socket address and port this socket is bound to.
    • isBound

      public boolean isBound()
      Returns whether this server socket is bound to a local address and port or not.
      Returns:
      true if this socket is bound, false otherwise.
    • isClosed

      public boolean isClosed()
      Returns whether this server socket is closed or not.
      Returns:
      true if this socket is closed, false otherwise.
    • setReuseAddress

      public void setReuseAddress​(boolean reuse) throws SocketException
      Sets the value for the socket option SocketOptions.SO_REUSEADDR.
      Parameters:
      reuse - the socket option setting.
      Throws:
      SocketException - if an error occurs while setting the option value.
    • getReuseAddress

      public boolean getReuseAddress() throws SocketException
      Gets the value of the socket option SocketOptions.SO_REUSEADDR.
      Returns:
      true if the option is enabled, false otherwise.
      Throws:
      SocketException - if an error occurs while reading the option value.
    • setReceiveBufferSize

      public void setReceiveBufferSize​(int size) throws SocketException
      Sets this socket's receive buffer size.
      Throws:
      SocketException
    • getReceiveBufferSize

      public int getReceiveBufferSize() throws SocketException
      Returns this socket's receive buffer size.
      Throws:
      SocketException
    • getChannel

      public ServerSocketChannel getChannel()
      Returns this socket's ServerSocketChannel, if one exists. A channel is available only if this socket wraps a channel. (That is, you can go from a channel to a socket and back again, but you can't go from an arbitrary socket to a channel.) In practice, this means that the socket must have been created by ServerSocketChannel.open().
    • setPerformancePreferences

      public void setPerformancePreferences​(int connectionTime, int latency, int bandwidth)
      Sets performance preferences for connection time, latency and bandwidth.

      This method does currently nothing.

      Parameters:
      connectionTime - the value representing the importance of a short connecting time.
      latency - the value representing the importance of low latency.
      bandwidth - the value representing the importance of high bandwidth.