Java Socket setKeepAlive() method

The setKeepAlive() method of Java Socket class returns a Boolean value 'true' if the write-half of the socket connection has been closed successfully else it returns false.

Syntax

Parameter

The parameter 'on' represents whether the socket should be enabled or not.

Return

NA

Throws

SocketException - if an error is there in the underlying protocol, such as a TCP error.

Example 1

Test it Now

Output:

SO_KEEPALIVE is enabled: true

Example 2

Test it Now

Output:

SO_KEEPALIVE is not enabled...

Example 3

Test it Now

Output:

Exception in thread "main" java.net.SocketException: Socket is closed
	at java.net.Socket.setKeepAlive(Socket.java:1309)
	at com.javaTpoint.JavaSocketSetKeepAliveExample3.main(JavaSocketSetKeepAliveExample3.java:13)




Latest Courses