[Ilugc] SO_REUSEADDR in conjunction with INADDR_ANY issue

  • From: cnarendra_babu@xxxxxxxxx (narendra babu)
  • Date: Fri, 29 Jul 2011 02:16:19 +0530 (IST)

Hello all ,

How can we allow a server to bind to a port that appears to be held open by a 
client process?? The netstat below indicates that port 9521 is not being 
listened
to by any server process:

$netstat -an | grep 9521
tcp??????? 0????? 0 127.0.0.1:9521????????????? 127.0.0.1:9521????????????? 
ESTABLISHED
tcp??????? 0????? 0 10.1.235.31:47776?????????? 10.1.19.219:9521??????????? 
ESTABLISHED
?
We are using SO_REUSEADDR in the server code, which on Solaris always allowed 
to re-bind to the port in question.
????? int on = 1;
????? if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
???????? printf("setsockopt SO_REUSEADDR failed: port=%d fd=%d", on, fd);
???????? 
???????? 
On Linux there appears to be a caveat with using SO_REUSEADDR in conjunction 
with INADDR_ANY.? This is from the Linux socket man page: 
?????? SO_REUSEADDR
????????????? Indicates that the rules used in validating addresses supplied
????????????? in a bind(2) call should allow reuse of local? addresses.? For
????????????? PF_INET sockets this means that a socket may bind, except when
????????????? there is an active listening? socket? bound? to? the? address.
????????????? When? the? listening socket is bound to INADDR_ANY with a spe-
????????????? cific port then it is not possible to bind to? this? port? for
????????????? any local address.
despite the fact that no server can bind the port, no new client can establish 
a connection to the port.? 
Also, the client that is established to the port is? sending messages to what 
it believes is its peer. 
No errors are returned.? 
The socket has been in this state for couple of? a days
?
Please advise ,
?
Thanks for your time and inputs
?
Thanks

?

Other related posts: