Java Socket getKeepAlive() method

The getKeepAlive() method of Java Socket class tests whether SO_KEEPALIVE option is enabled or not.

Syntax

Parameter

NA

Return

The getKeepAlive() method either returns a Boolean indicating whether SO_KEEPALIVE option is enabled or not.

Throws

SocketException - if there is an error 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 enabled: false

Example 3

Test it Now

Output:

Exception in thread "main" java.net.SocketException: Socket is closed
	at java.net.Socket.getKeepAlive(Socket.java:1325)
	at com.javaTpoint.JavaSocketGetKeepAliveExample3.main(JavaSocketGetKeepAliveExample3.java:11)




Latest Courses