Java Socket getReuseAddress () method

The getReuseAddress () method of Java Socket class tests whether the SO_REUSEADDR option is enabled or not. It throws SocketException if finds an error in the underlying protocol, such as a TCP error.

Syntax

Parameter

NA

Return

The getReuseAddress () method returns a Boolean value 'true' if the SO_REUSEADDR is enabled else it returns false.

Throws

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

Example 1

Test it Now

Output:

SO_REUSEADDR option is enabled: true

Example 2

Test it Now

Output:

SO_REUSEADDR option is enabled: false

Example 3

Test it Now

Output:

Exception in thread "main" java.net.SocketException: Socket is closed
	at java.net.Socket.setReuseAddress(Socket.java:1448)
	at com.javaTpoint.JavaSocketGetReuseAddressExample3.main(JavaSocketGetReuseAddressExample3.java:13)




Latest Courses