Sapien Blogs

Know the Unknown

Java socket properties for tuning and resiliency

Socket Connection Timeout

See Socket Api
public void connect(SocketAddress endpoint,int timeout) throws IOException

Socket Read Timeout/  SoTimeout

Timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets
See Socket API
public void setSoTimeout(int timeout) throws SocketException

Send BufferSize
Receive BufferSize

Socket Keep Alive

You can't send keep-alive on demand, and for most operating systems the TCP keepalive timeout is only configurable on a system-wide level and set far too high to be generally useful for applications. In Java you can turn on keep alive for a socket, but you cant configure keep-alive timing. SO_KEEPALIVE is implemented in the OS network protocol stacks without sending any "real" data. The keep-alive interval is operating system dependent, and may be tuneable via a kernel parameter.

See Socket Api
public void setKeepAlive(boolean on) throws SocketException

Java 7 Sockets Direct Protocol(SDP)

Write directly to Infiniband. By configuring the Java VM’s specific join-point to the InfiniBand OS device driver and libraries (aka InfiniBand’s VERBs layer API) the application code’s use of* and* - which is Java’s API for Transport Layer OS resources (OSI layer 4) can by-pass the traditional network protocol stack (i.e. it can by-pass OSI layer 3 and bypass OSI layer 2) and go *directly* to InfiniBand (OSI layer 1). The performance implications and payoffs are very significant.


Http Keep-Alive is used for Http connection reuse  or persistenet connections. If keep-alive is set, after a http request-response is completed, Java will reuse the underlying socket/TCP connection for another http request-response. Keep-alive is set by "keepAlive:true" http header and terminated by "Connection: close" header. In order to determine the end of a request/response, each response should have a Content-Length header set, or each chunk start with size in case of chunked transfer encoding. Java will clean-up the connection and reuse the socket after the application has finished reading  the response body or made a close() on the URLConnection. See more details -

JDBC Connection

MySql JDBC Driver - See connection imeout and read timeout as connection properties in
Oracle - oracle.jdbc.ReadTimeout property and others- see driver documentation
SQLServer - see jTDS driver or Microsoft driver documentation.

Half Open, Half-Closed Connections

See socket Apis. Causes FIN to be sent to the other side as one way close.
public void shutdownOutput() throws IOException

See other shutdownInput() which places the socket InputStream at end of stream. Any data sent to the input stream side of the socket is acknowledged and then silently discarded.

Linger Option - Orderly vs. Abortive connection close in Java

Orderly close -socket.close() without 0 linger timout causes socket to shutdown orderly. This sends a FIN to the other side of the TCP connection, which means finished sending. However the other side of the TCP connection may continue sending data because only one side of the stream is closed. Since the socket is closed on this side, the data other side had sent after the close will be lost. One solution is to call linger timeout 0 before close() which causes abandoning the socket by stopping both read and write( the write buffer still may have some data to send). Other option(clean) is to wait for read to finish before calling close(). Else consider calling shutdownOutput().
Abortive close- call setSoLinger(true,0) before calling close(). This will send RST instead of FIN to the other side of the TCP connection.


TCP Lifecycle

Remote, Client* Local, Server*
*or vice-versa, though requests typically originate at clients.
2 Listening
Awaiting connection request.
3 Syn-Sent
Sent connection-request.1
Awaiting acknowledgement.1
Awaiting connection-request.2
Received acknowledgement.1
Received connection-request.2
Sent acknowledgement.2

4 Syn-Received
Received connection-request.1
Sent acknowledgement.1
Sent connection-request.2
Awaiting acknowledgement.2
5 Established
The connection is open.
Data moves both directions.
5 Established
Received acknowledgement.2
The connection is open.
Data moves both directions.
6 Fin-Wait.1
Sent close-request.a
Awaiting acknowledgement.a
Awaiting close-request.b
8 Close-Wait
Received close-request.a
Sent acknowledgement.a
When finished sending data,
will send close-request.b
7 Fin-Wait.2
Received acknowledgement.a
Still awaiting close-request.b
10 Closing
Received close-request.b
Sent acknowledgement.b
Still awaiting acknowledgement.a
9 Last-Ack
Sent close-request.b
Awaiting acknowledgement.b
11 Time-Wait
Received acknowledgement.a
Received close-request.b
Sent acknowledgement.b
Allowing time for delivery
of acknowledgement.b
1 Closed
A "fictional" state;
there is no connection.
2 Listening
Awaiting connection request.




Post a Comment:
Comments are closed for this entry.