[Ilugc] SO_REUSEADDR in conjunction with INADDR_ANY issue

  • From: cnarendra_babu@xxxxxxxxx (narendra babu)
  • Date: Sat, 30 Jul 2011 21:37:19 +0530 (IST)





Hello all ,

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

$netstat -an | grep 9521
tcp 0 0 127.0.0.1:9521 =A0 127.0.0.1:9521 =A0 ESTABLISHED
tcp 0 0 10.1.235.31:47776 =A0 10.1.19.219:9521 ESTABL=SHED

Ok this should be in FIN_WAIT or FIN_WAIT2 or something.

If it is connected how will the socket be free?

We are using SO_REUSEADDR in the server code, which on Solaris always all=wed 
to re-bind to the port in question.
int on = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(=n)) == -1)
printf("setsockopt SO_REUSEADDR failed: port=%= fd=%d", on, fd);


On Linux there appears to be a caveat with using SO_REUSEADDR in conjunct=on 
with INADDR_ANY. This is from the Linux socket man page:
SO_REUSEADDR
Indicates that the rules used in =alidating addresses supplied
in a bind(2) call should allow re=se of local addresses. For
PF_INET sockets this means that a=socket may bind, except when
there is an active listening s=cket bound to the address.
When the listening socket i= bound to INADDR_ANY with a spe-
cific port then it is not possibl= to bind to this port for
any local address. despite the fact that no server can bind the port, no new 
client can esta=lish a connection to the port.
Also, the client that is established to the port is sending messages t= what 
it believes is its peer.? No errors are returned. The socket has been in this 
state for couple of a days

I know this question really really well.
?
Do you have solution in Linux working ?, it works fine in Solaris .

I have faced this problem several times.

But I still can't answer it. I use

setsockopt( ... SO_REUSEADDR..)
?
I used the setsockopt( ... SO_REUSEADDR..) in my code , still its not working , 
still the same issue 
and
setsockopt(...SO_REUSEPORT..)
?
?I believe SO_REUSEPORT is not portable , its more used in BSD Unixes and i 
believe that its functionality is covered by SO_REUSEADDR under linux as per 
manual 

I have tried? couple of more options for reusing the port by setting the kernel 
parameters (TCP_TW_RECYCLE and TCP_TW_REUSE)? , not much luck here also .
?
I? did do good amount of reserach online before posting ,? but could not get 
much info to resolve 
http://hea-www.harvard.edu/~fine/Tech/addrinuse.html
http://lkml.indiana.edu/hypermail/linux/kernel/9902.1/0922.html
http://stackoverflow.com/questions/3127990/linux-sockets-server-exits-before-client
https://www.ibm.com/developerworks/library/l-sockpit/
?
Girish once gain many? thanks for providing your pointers? and rellly 
appriciate your time 
?
Thanks for your inputs in advance 

Thanks
Narendra


Other related posts: