Skip to content

Most visited

Recently visited

navigation
Added in API level 1

ServerSocket

public class ServerSocket
extends Object implements Closeable

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


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 using a wildcard address.

ServerSocket(int port, int backlog)

Constructs a new ServerSocket instance bound to the given port using a wildcard address.

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 if this socket has ever been bound, null otherwise.

int getLocalPort()

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

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.

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.

void setSoTimeout(int timeout)

Sets the accept timeout in milliseconds for this socket.

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.

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public constructors

ServerSocket

Added in API level 1
ServerSocket ()

Constructs a new unbound ServerSocket.

Throws
IOException if an error occurs while creating the socket.

ServerSocket

Added in API level 1
ServerSocket (int port)

Constructs a new ServerSocket instance bound to the given port using a wildcard address. The backlog is set to 50. If port == 0, a port will be assigned by the OS.

Parameters
port int
Throws
IOException if an error occurs while creating the socket.

ServerSocket

Added in API level 1
ServerSocket (int port, 
                int backlog)

Constructs a new ServerSocket instance bound to the given port using a wildcard address. The backlog is set to backlog. If port == 0, a port will be assigned by the OS.

Parameters
port int
backlog int
Throws
IOException if an error occurs while creating the socket.

ServerSocket

Added in API level 1
ServerSocket (int port, 
                int backlog, 
                InetAddress localAddress)

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.

Parameters
port int
backlog int
localAddress InetAddress
Throws
IOException if an error occurs while creating the socket.

Public methods

accept

Added in API level 1
Socket accept ()

Waits for an incoming request and blocks until the connection is opened. This method returns a socket object representing the just opened connection.

Returns
Socket the connection representing socket.
Throws
IOException if an error occurs while accepting a new connection.

bind

Added in API level 1
void bind (SocketAddress localAddr)

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 SocketAddress: 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

Added in API level 1
void bind (SocketAddress localAddr, 
                int backlog)

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 SocketAddress: the local machine address and port to bind on.
backlog int: 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.

close

Added in API level 1
void close ()

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.

getChannel

Added in API level 1
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 open().

Returns
ServerSocketChannel

getInetAddress

Added in API level 1
InetAddress getInetAddress ()

Gets the local IP address of this server socket if this socket has ever been bound, null otherwise. This is useful for multihomed hosts.

Returns
InetAddress the local address of this server socket.

getLocalPort

Added in API level 1
int getLocalPort ()

Gets the local port of this server socket or -1 if the socket is not bound. If the socket has ever been bound this method will return the local port it was bound to, even after it has been closed.

Returns
int the local port this server is listening on.

getLocalSocketAddress

Added in API level 1
SocketAddress getLocalSocketAddress ()

Gets the local socket address of this server socket or null if the socket is unbound. This is useful on multihomed hosts. If the socket has ever been bound this method will return the local address it was bound to, even after it has been closed.

Returns
SocketAddress the local socket address and port this socket is bound to.

getReceiveBufferSize

Added in API level 1
int getReceiveBufferSize ()

Returns this socket's receive buffer size.

Returns
int
Throws
SocketException

getReuseAddress

Added in API level 1
boolean getReuseAddress ()

Gets the value of the socket option SocketOptions.SO_REUSEADDR.

Returns
boolean true if the option is enabled, false otherwise.
Throws
SocketException if an error occurs while reading the option value.

getSoTimeout

Added in API level 1
int getSoTimeout ()

Gets the socket accept timeout.

Returns
int
Throws
IOException if the option cannot be retrieved.

isBound

Added in API level 1
boolean isBound ()

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

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

isClosed

Added in API level 1
boolean isClosed ()

Returns whether this server socket is closed or not.

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

setPerformancePreferences

Added in API level 1
void setPerformancePreferences (int connectionTime, 
                int latency, 
                int bandwidth)

Sets performance preferences for connection time, latency and bandwidth.

This method does currently nothing.

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

setReceiveBufferSize

Added in API level 1
void setReceiveBufferSize (int size)

Sets this socket's receive buffer size.

Parameters
size int
Throws
SocketException

setReuseAddress

Added in API level 1
void setReuseAddress (boolean reuse)

Sets the value for the socket option SocketOptions.SO_REUSEADDR.

Parameters
reuse boolean: the socket option setting.
Throws
SocketException if an error occurs while setting the option value.

setSoTimeout

Added in API level 1
void setSoTimeout (int timeout)

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 int: the timeout in milliseconds or 0 for no timeout.
Throws
SocketException if an error occurs while setting the option.

setSocketFactory

Added in API level 1
void setSocketFactory (SocketImplFactory aFactory)

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 SocketImplFactory: the streaming socket factory to be used for further socket instantiations.
Throws
IOException if the factory could not be set or is already set.

toString

Added in API level 1
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.

Returns
String the textual socket representation.

Protected methods

implAccept

Added in API level 1
void implAccept (Socket aSocket)

Invokes the server socket implementation to accept a connection on the given socket aSocket.

Parameters
aSocket Socket: the concrete SocketImpl to accept the connection request on.
Throws
IOException if the connection cannot be accepted.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.