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 unbound ServerSocket.
ServerSocket(int port)
Constructs a new ServerSocket instance bound to the given port.
ServerSocket(int port, int backlog)
Constructs a new ServerSocket instance bound to the given port.
ServerSocket(int port, int backlog, InetAddress localAddress)
Constructs a new ServerSocket instance bound to the given localAddress 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 with a maximum backlog of 50 unaccepted connections.
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()
Returns this socket's ServerSocketChannel, if one exists.
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()
Returns this socket's receive buffer size.
boolean getReuseAddress()
Gets the value of the socket option SocketOptions.SO_REUSEADDR.
synchronized int getSoTimeout()
Gets the socket accept timeout.
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 this socket's receive buffer size.
void setReuseAddress(boolean reuse)
Sets the value for the socket option SocketOptions.SO_REUSEADDR.
synchronized void setSoTimeout(int timeout)
Sets the accept timeout in milliseconds for this 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 unbound ServerSocket.

Throws
IOException if an error occurs while creating the socket.

public ServerSocket (int port)

Since: API Level 1

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.

public ServerSocket (int port, int backlog)

Since: API Level 1

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.

public ServerSocket (int port, int backlog, InetAddress localAddress)

Since: API Level 1

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.

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

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.

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.

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

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 open().

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

Returns this socket's receive buffer size.

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 socket accept timeout.

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 this socket's receive buffer size.

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

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.