java.net

Class ServerSocket

public class ServerSocket extends Object

This class models server side sockets. The basic model is that the server socket is created and bound to some well known port. It then listens for and accepts connections. At that point the client and server sockets are ready to communicate with one another utilizing whatever application layer protocol they desire. As with the Socket class, most instance methods of this class simply redirect their calls to an implementation class.
Constructor Summary
ServerSocket()
Constructor that simply sets the implementation.
ServerSocket(int port)
Creates a server socket and binds it to the specified port.
ServerSocket(int port, int backlog)
Creates a server socket and binds it to the specified port.
ServerSocket(int port, int backlog, InetAddress bindAddr)
Creates a server socket and binds it to the specified port.
Method Summary
Socketaccept()
Accepts a new connection and returns a connected Socket instance representing that connection.
voidbind(SocketAddress endpoint)
Binds the server socket to a specified socket address
voidbind(SocketAddress endpoint, int backlog)
Binds the server socket to a specified socket address
voidclose()
Closes this socket and stops listening for connections
ServerSocketChannelgetChannel()
Returns the unique ServerSocketChannel object associated with this socket, if any.
InetAddressgetInetAddress()
This method returns the local address to which this socket is bound
intgetLocalPort()
This method returns the local port number to which this socket is bound
SocketAddressgetLocalSocketAddress()
Returns the local socket address
intgetReceiveBufferSize()
This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers.
booleangetReuseAddress()
Checks if the SO_REUSEADDR option is enabled
intgetSoTimeout()
Retrieves the current value of the SO_TIMEOUT setting.
protected voidimplAccept(Socket socket)
This protected method is used to help subclasses override ServerSocket.accept().
booleanisBound()
Returns true when the socket is bound, otherwise false
booleanisClosed()
Returns true if the socket is closed, otherwise false
voidsetReceiveBufferSize(int size)
This method sets the value for the system level socket option SO_RCVBUF to the specified value.
voidsetReuseAddress(boolean on)
Enables/Disables the SO_REUSEADDR option
static voidsetSocketFactory(SocketImplFactory fac)
Sets the SocketImplFactory for all ServerSocket's.
voidsetSoTimeout(int timeout)
Sets the value of SO_TIMEOUT.
StringtoString()
Returns the value of this socket as a String.

Constructor Detail

ServerSocket

public ServerSocket()
Constructor that simply sets the implementation.

Throws: IOException If an error occurs

UNKNOWN: This constructor is public since JDK 1.4

ServerSocket

public ServerSocket(int port)
Creates a server socket and binds it to the specified port. If the port number is 0, a random free port will be chosen. The pending connection queue on this socket will be set to 50.

Parameters: port The port number to bind to

Throws: IOException If an error occurs SecurityException If a security manager exists and its checkListen method doesn't allow the operation

ServerSocket

public ServerSocket(int port, int backlog)
Creates a server socket and binds it to the specified port. If the port number is 0, a random free port will be chosen. The pending connection queue on this socket will be set to the value passed as arg2.

Parameters: port The port number to bind to backlog The length of the pending connection queue

Throws: IOException If an error occurs SecurityException If a security manager exists and its checkListen method doesn't allow the operation

ServerSocket

public ServerSocket(int port, int backlog, InetAddress bindAddr)
Creates a server socket and binds it to the specified port. If the port number is 0, a random free port will be chosen. The pending connection queue on this socket will be set to the value passed as backlog. The third argument specifies a particular local address to bind t or null to bind to all local address.

Parameters: port The port number to bind to backlog The length of the pending connection queue bindAddr The address to bind to, or null to bind to all addresses

Throws: IOException If an error occurs SecurityException If a security manager exists and its checkListen method doesn't allow the operation

Since: 1.1

Method Detail

accept

public Socket accept()
Accepts a new connection and returns a connected Socket instance representing that connection. This method will block until a connection is available.

Returns: socket object for the just accepted connection

Throws: IOException If an error occurs SecurityException If a security manager exists and its checkListen method doesn't allow the operation IllegalBlockingModeException If this socket has an associated channel, and the channel is in non-blocking mode SocketTimeoutException If a timeout was previously set with setSoTimeout and the timeout has been reached

bind

public void bind(SocketAddress endpoint)
Binds the server socket to a specified socket address

Parameters: endpoint The socket address to bind to

Throws: IOException If an error occurs IllegalArgumentException If address type is not supported SecurityException If a security manager exists and its checkListen method doesn't allow the operation

Since: 1.4

bind

public void bind(SocketAddress endpoint, int backlog)
Binds the server socket to a specified socket address

Parameters: endpoint The socket address to bind to backlog The length of the pending connection queue

Throws: IOException If an error occurs IllegalArgumentException If address type is not supported SecurityException If a security manager exists and its checkListen method doesn't allow the operation

Since: 1.4

close

public void close()
Closes this socket and stops listening for connections

Throws: IOException If an error occurs

getChannel

public ServerSocketChannel getChannel()
Returns the unique ServerSocketChannel object associated with this socket, if any.

The socket only has a ServerSocketChannel if its created by ServerSocketChannel.open().

Returns: the associated socket channel, null if none exists

Since: 1.4

getInetAddress

public InetAddress getInetAddress()
This method returns the local address to which this socket is bound

Returns: The socket's local address

getLocalPort

public int getLocalPort()
This method returns the local port number to which this socket is bound

Returns: The socket's port number

getLocalSocketAddress

public SocketAddress getLocalSocketAddress()
Returns the local socket address

Returns: the local socket address, null if not bound

Since: 1.4

getReceiveBufferSize

public int getReceiveBufferSize()
This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers.

Returns: The receive buffer size.

Throws: SocketException If an error occurs or Socket is not connected

Since: 1.4

getReuseAddress

public boolean getReuseAddress()
Checks if the SO_REUSEADDR option is enabled

Returns: true if SO_REUSEADDR is set, false otherwise

Throws: SocketException If an error occurs

Since: 1.4

getSoTimeout

public int getSoTimeout()
Retrieves the current value of the SO_TIMEOUT setting. A value of 0 implies that SO_TIMEOUT is disabled (ie, operations never time out). This is the number of milliseconds a socket operation can block before an InterruptedIOException is thrown.

Returns: The value of SO_TIMEOUT

Throws: IOException If an error occurs

Since: 1.1

implAccept

protected final void implAccept(Socket socket)
This protected method is used to help subclasses override ServerSocket.accept(). The passed in socket will be connected when this method returns.

Parameters: socket The socket that is used for the accepted connection

Throws: IOException If an error occurs IllegalBlockingModeException If this socket has an associated channel, and the channel is in non-blocking mode

Since: 1.1

isBound

public boolean isBound()
Returns true when the socket is bound, otherwise false

Returns: true if socket is bound, false otherwise

Since: 1.4

isClosed

public boolean isClosed()
Returns true if the socket is closed, otherwise false

Returns: true if socket is closed, false otherwise

Since: 1.4

setReceiveBufferSize

public void setReceiveBufferSize(int size)
This method sets the value for the system level socket option SO_RCVBUF to the specified value. Note that valid values for this option are specific to a given operating system.

Parameters: size The new receive buffer size.

Throws: SocketException If an error occurs or Socket is not connected IllegalArgumentException If size is 0 or negative

Since: 1.4

setReuseAddress

public void setReuseAddress(boolean on)
Enables/Disables the SO_REUSEADDR option

Parameters: on true if SO_REUSEADDR should be enabled, false otherwise

Throws: SocketException If an error occurs

Since: 1.4

setSocketFactory

public static void setSocketFactory(SocketImplFactory fac)
Sets the SocketImplFactory for all ServerSocket's. This may only be done once per virtual machine. Subsequent attempts will generate an exception. Note that a SecurityManager check is made prior to setting the factory. If insufficient privileges exist to set the factory, an exception will be thrown

Parameters: fac the factory to set

Throws: SecurityException If this operation is not allowed by the SecurityManager. SocketException If the factory object is already defined IOException If any other error occurs

setSoTimeout

public void setSoTimeout(int timeout)
Sets the value of SO_TIMEOUT. A value of 0 implies that SO_TIMEOUT is disabled (ie, operations never time out). This is the number of milliseconds a socket operation can block before an InterruptedIOException is thrown.

Parameters: timeout The new SO_TIMEOUT value

Throws: SocketException If an error occurs

Since: 1.1

toString

public String toString()
Returns the value of this socket as a String.

Returns: This socket represented as a String.