Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is SO_REUSEADDR socket options useful on the client-side?

Tags:

linux

sockets

I came across the sentence in one java client library:

socket.setReuseAddress(true);

Thought this is used to improve performance, since the SO_REUSEADDR option can indicate that socket can forcibly use the TIME_WAIT port even if it belongs to the other socket.

But Also I found that this option is mostly used in the server-side, to enable the server restarting quickly, not waiting the TIME_WAIT socket to close.

My question is that Is this option useful for the client-side, like this client library? Will this do harmful to the other socket, like some attack?

Thanks a lot!

-Dimi

like image 331
haonan hu Avatar asked Oct 17 '22 18:10

haonan hu


1 Answers

It depends on what you mean by "client". You also mention "client library", which has nothing to do with it.

This is often misunderstood, SO_REUSEADDR is to be able to reuse a socket in TIME_WAIT, and TIME_WAIT only happens on one side of the TCP connection, the one that initiates the termination sequence i.e. sends the first FIN packet i.e. calls shutdown(SHUT_WR) first or calls close first, although the latter is unclear/may depend on other things such as connection state or platform, reasons why you should not call close before first calling shutdown(SHUT_WR). This article is a very informative as well as the two referenced at the end of the article. It makes clear that TIME_WAIT may occur on the listening (server) side as well as client side, and recommends actually having clients always initiate termination ("active close") so that the server doesn't accumulate sockets in TIME_WAIT, where that would be more of a problem.

like image 88
haelix Avatar answered Oct 21 '22 03:10

haelix