Useful Methods of ServerSocket Class

The following sections give a summary of important methods of ServerSocket class.

1. Constructors

ServerSocket()

Creates a raw server socket which is not yet bound to a specific socket address (IP address and port). However, the binding may be done later using bind() method.

ServerSocket(int port)

Creates a server socket and bounds it to the specified port. If 0 is specified, any free port is used to bind the socket object. The method getLocaiPort() may be used to retrieve this port number. This socket may be referred to by any IP address of the computer. The default queue length for this socket is 50.

ServerSocket(int port, int queueLength)

Creates a server socket having the specified queueLength and bounds the socket to the specified port. If 0 is specified for the port, any free port is used to bind the socket object. The method getLocalPort() may be used to retrieve this port number. If the value of queueLength is less than or equal to 0, the default value (50) is assumed. This socket may be referred to by any IP address of the computer.

ServerSocket(int port, int queueLength, InetAddress address)

Creates a server socket having the specified queueLength and bounds the socket to the specified port and address. If 0 is specified for the port, any free port is used to bind the socket object. The method getLocalPort() may be used to retrieve this port number. If the value of queueLength is less than or equal to 0, the default value (50) is assumed. The socket listens only on the specified IP address.

2. Methods

Socket accept()

Processes client requests from the queue associated with the ServerSocket object in First-Come- First-Served basis. When there are no requests in the queue, this method waits (blocks the caller) for further incoming connections. Upon receiving a request, it creates a virtual channel between the server and client and returns a Socket object that represents the sever-side end of the virtual channel.

void bind(SocketAddress address)

Binds the socket to the specified address (IP address and port). This method expects address as InetSocketAddress object. The InetSocketAddress class extends SocketAddress class and implements an IP Socket Address as [IP address, port number] or [hostname, port number] pair. In the latter case, an attempt will be made to resolve the hostname. If resolution fails then the address is said to be unresolved and a SocketException is thrown. However, the name can still be used in some circumstances such as connecting through a proxy. If the address is null, bind() picks up an ephemeral port and a valid local address to bind the socket. The default queue length for this socket is 50.

void bind(SocketAddress address, int queueLength)

Same as previous version except that the length of the queue is set to the specified queueLength. If the value of queueLength is less than or equal to 0, the default value (50) is assumed.

InetAddress getInetAddress()

Returns the address to which the socket is bound or null if the socket is not yet bound.

int getLocalPort()

Returns the port number this socket is bound or -1 if the socket is not bound yet.

SocketAddress getLocalSocketAddress ()

Returns actually an InetSocketAddress object that represents the socket address (IP address and port) of the endpoint of this socket or null if the socket is not yet bound.

boolean isBound()

Returns true if the socket is successfully bound to a socket address, false otherwise.

boolean isClosed()

Returns true if the socket is closed, false otherwise.

int getSoTimeout()

Returns SocketOptions.SO_TIMEOUT value. The value 0 indicates that timeout option is disabled (i.e. infinite timeout).

void setSoTimeout(int timeout)

Sets the SocketOptions.SO_TIMEOUT value with the specified timeout in milliseconds. If a non­zero timeout value is specified and subsequently accept() method is called, the accept() method waits for only a specified amount of time for the incoming connection request. If no connection request comes within this specified time, a SocketTimeoutException is thrown. However, the socket remains valid and a different timeout value may be used (if required) later and accept() method may also be called subsequently to have its effect. The timeout value is negative, an IllegalArgumentException is raised. A timeout value of 0 (which is also the default timeout) is used for infinite timeout.

void setReuseAddress(boolean status)

Sets SocketOptions.SO_REUSEADDR value to true or false. If a ServerSocket object is closed using close() method, the connection is not closed and the address and port are not freed immediately. It may remain in a timeout state temporarily keeping the connection as it is. This state is known as time_wait state. Note that TCP provides in order delivery of packets. However, since the underlying IP layer does not provide any such guarantee on ordering, IP packets may arrive out of order. The task of TCP layer is to arrange them in the correct order and forward them to the application layer. So, out of order packets may arrive even if the connection is closed. That is why the connection is kept open (temporarily) so that delayed packets may be handled appropriately. So, time_wait state basically means that one side closed the connection but the final confirmation of the close is pending. The connection is finally terminated and all resources are freed. However, it may not be possible for an application to use the same address and port, if there is a connection in the time_wait state involving the address and port. The setReuseAddress() method may be used to enable/disable so_reuseaddr option prior to binding the socket using bind() to allow/disallow the socket to be bound even though a previous connection is in a time_wait state.

boolean getReuseAddress()

Returns a true/false indicating whether or not so_reuseaddr is enabled/disabled.

ServerSocketChannel getChannel()

Returns a ServerSocketChannel object (if any) associated with this socket provided that the channel itself was created using static open() method of ServerSocketChannel class.

void close()

Closes the socket. If the socket has an associated channel, the channel is also closed. If a thread is currently blocked on accept() method, then the thread throws a SocketException.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *