Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is SocketOptionName.ReuseAddress used for?

Tags:

.net

tcp

sockets

I used to think that using SocketOptionName.ReuseAddress, I can reuse a port that is in TIME_WAIT state. But I tried to experiment with it and it seems it has no effect.

If I check sockets using netstat, and it shows the socket is in TIME_WAIT state and I immediately run the client again, I get the exception:

Only one usage of each socket address (protocol/network address/port) is normally permitted 172.16.16.16:12345

I cannot make anything out of it. Please can you elaborate what SocketOptionName.ReuseAddress is good for?

like image 484
Hemant Avatar asked May 04 '10 08:05

Hemant


1 Answers

Why are you binding your client port in the first place? You probably don't need to and if you don't then it's much better to allow the OS to select an ephemeral port for you and then you wouldn't have this problem in the first place.

Secondly, why do you think it's a good idea to reuse a socket that it's TIME_WAIT; the state exists for a valid reason...

As for why it's not working for you, take a look at the link that I posted in answer to this similar question: When binding a client TCP socket to a specific local port with Winsock, SO_REUSEADDR does not have any effect

like image 153
Len Holgate Avatar answered Nov 03 '22 09:11

Len Holgate