public class

ServerSocket

extends Object
java.lang.Object
   ↳ java.net.ServerSocket
Known Direct Subclasses

Class Overview

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.

Summary

Public Constructors
ServerSocket()
Constructs a new ServerSocket instance which is not bound to any port.
ServerSocket(int aport)
Constructs a new ServerSocket instance bound to the nominated port on the localhost.
ServerSocket(int aport, int backlog)
Constructs a new ServerSocket instance bound to the nominated port on the localhost.
ServerSocket(int aport, int backlog, InetAddress localAddr)
Constructs a new ServerSocket instance bound to the nominated local host address and port.
Public Methods
Socket accept()
Waits for an incoming request and blocks until the connection is opened.
void bind(SocketAddress localAddr)
Binds this server socket to the given local socket address.
void bind(SocketAddress localAddr, int backlog)
Binds this server socket to the given local socket address.
void close()
Closes this server socket and its implementation.
ServerSocketChannel getChannel()
Gets the related channel if this instance was created by a ServerSocketChannel.
InetAddress getInetAddress()
Gets the local IP address of this server socket or null if the socket is unbound.
int getLocalPort()
Gets the local port of this server socket or -1 if the socket is unbound.
SocketAddress getLocalSocketAddress()
Gets the local socket address of this server socket or null if the socket is unbound.
int getReceiveBufferSize()
Gets the value for the receive buffer size socket option SocketOptions.SO_RCVBUF.
boolean getReuseAddress()
Gets the value of the socket option SocketOptions.SO_REUSEADDR.
synchronized int getSoTimeout()
Gets the timeout period of this server socket.
boolean isBound()
Returns whether this server socket is bound to a local address and port or not.
boolean isClosed()
Returns whether this server socket is closed or not.
void setPerformancePreferences(int connectionTime, int latency, int bandwidth)
Sets performance preferences for connection time, latency and bandwidth.
void setReceiveBufferSize(int size)
Sets the server socket receive buffer size SocketOptions.SO_RCVBUF.
void setReuseAddress(boolean reuse)
Sets the value for the socket option SocketOptions.SO_REUSEADDR.
synchronized void setSoTimeout(int timeout)
Sets the timeout period of this server socket.
synchronized static void setSocketFactory(SocketImplFactory aFactory)
Sets the server socket implementation factory of this instance.
String toString()
Returns a textual representation of this server socket including the address, port and the state.
Protected Methods
final void implAccept(Socket aSocket)
Invokes the server socket implementation to accept a connection on the given socket aSocket.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public ServerSocket ()

Since: API Level 1

Constructs a new ServerSocket instance which is not bound to any port. The default number of pending connections may be backlogged.

Throws
IOException if an error occurs while creating the server socket.

public ServerSocket (int aport)

Since: API Level 1

Constructs a new ServerSocket instance bound to the nominated port on the localhost. The default number of pending connections may be backlogged. If aport is 0 a free port is assigned to the socket.

Parameters
aport the port number to listen for connection requests on.
Throws
IOException if an error occurs while creating the server socket.

public ServerSocket (int aport, int backlog)

Since: API Level 1

Constructs a new ServerSocket instance bound to the nominated port on the localhost. The number of pending connections that may be backlogged is specified by backlog. If aport is 0 a free port is assigned to the socket.

Parameters
aport the port number to listen for connection requests on.
backlog the number of pending connection requests, before requests will be rejected.
Throws
IOException if an error occurs while creating the server socket.

public ServerSocket (int aport, int backlog, InetAddress localAddr)

Since: API Level 1

Constructs a new ServerSocket instance bound to the nominated local host address and port. The number of pending connections that may be backlogged is specified by backlog. If aport is 0 a free port is assigned to the socket.

Parameters
aport the port number to listen for connection requests on.
backlog the number of pending connection requests, before requests will be rejected.
localAddr the local machine address to bind on.
Throws
IOException if an error occurs while creating the server socket.

Public Methods

public Socket accept ()

Since: API Level 1

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.

public void bind (SocketAddress localAddr)

Since: API Level 1

Binds this server socket to the given local socket address. The default number of pending connections may be backlogged. 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.

public void bind (SocketAddress localAddr, int backlog)

Since: API Level 1

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. The value for backlog must e greater than 0 otherwise the default value will be used.

Parameters
localAddr the local machine address and port to bind on.
backlog the number of pending connection requests, before requests will be rejected.
Throws
IllegalArgumentException if the SocketAddress is not supported.
IOException if the socket is already bound or a problem occurs during binding.

public void close ()

Since: API Level 1

Closes this server socket and its implementation. Any attempt to connect to this socket thereafter will fail.

Throws
IOException if an error occurs while closing this socket.

public ServerSocketChannel getChannel ()

Since: API Level 1

Gets the related channel if this instance was created by a ServerSocketChannel. The current implementation returns always null.

Returns
  • the related ServerSocketChannel if any.

public InetAddress getInetAddress ()

Since: API Level 1

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.

public int getLocalPort ()

Since: API Level 1

Gets the local port of this server socket or -1 if the socket is unbound.

Returns
  • the local port this server is listening on.

public SocketAddress getLocalSocketAddress ()

Since: API Level 1

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.

public int getReceiveBufferSize ()

Since: API Level 1

Gets the value for the receive buffer size socket option SocketOptions.SO_RCVBUF.

Returns
  • the receive buffer size of this socket.
Throws
SocketException if an error occurs while reading the option value.

public boolean getReuseAddress ()

Since: API Level 1

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.

public synchronized int getSoTimeout ()

Since: API Level 1

Gets the timeout period of this server socket. This is the time the server will wait listening for accepted connections before exiting.

Returns
  • the listening timeout value of this server socket.
Throws
IOException if the option cannot be retrieved.

public boolean isBound ()

Since: API Level 1

Returns whether this server socket is bound to a local address and port or not.

Returns
  • true if this socket is bound, false otherwise.

public boolean isClosed ()

Since: API Level 1

Returns whether this server socket is closed or not.

Returns
  • true if this socket is closed, false otherwise.

public void setPerformancePreferences (int connectionTime, int latency, int bandwidth)

Since: API Level 1

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.

public void setReceiveBufferSize (int size)

Since: API Level 1

Sets the server socket receive buffer size SocketOptions.SO_RCVBUF.

Parameters
size the buffer size in bytes.
Throws
SocketException if an error occurs while setting the size or the size is invalid.

public void setReuseAddress (boolean reuse)

Since: API Level 1

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.

public synchronized void setSoTimeout (int timeout)

Since: API Level 1

Sets the timeout period of this server socket. This is the time the server will wait listening for accepted connections before exiting. This value must be a positive number.

Parameters
timeout the listening timeout value of this server socket.
Throws
SocketException if an error occurs while setting the option.

public static synchronized void setSocketFactory (SocketImplFactory aFactory)

Since: API Level 1

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.

public String toString ()

Since: API Level 1

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.

Returns
  • the textual socket representation.

Protected Methods

protected final void implAccept (Socket aSocket)

Since: API Level 1

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.