Java Socket setReuseAddress () method

The setReuseAddress () method of Java Socket class enables or disables the SO_REUSEADDR socket option. The initial setting of SO_REUSEADDR is disabled.

Syntax

Parameter

The parameter 'on' states whether the SO_REUSEADDR option should be enabled or disabled.

Return

NA

Throws

SocketException - if the socket is closed or an error occurs while enabling or disabling the SO_REUSEADDR socket option.

Example 1

Test it Now

Output:

SO_REUSEADDR option is enabled: true

Example 2

Test it Now

Output:

SO_REUSEADDR option is disabled

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.JavaSocketSetReuseAddressExample3.main(JavaSocketSetReuseAddressExample3.java:15)




Latest Courses