[Ilugc] SO_REUSEADDR in conjunction with INADDR_ANY issue

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Fri, 29 Jul 2011 09:26:48 +0530

On Fri, Jul 29, 2011 at 2:16 AM, narendra babu <cnarendra_babu at yahoo.com> 
wrote:

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


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 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

I know this question really really well.

I have faced this problem several times.

But I still can't answer it. I use

setsockopt( ... SO_REUSEADDR..)

and

setsockopt(...SO_REUSEPORT..)

under linux.

It is something similar for Solaris, FreeBSD, NetBSD, OpenBSD or any other UNIX.

Now if you don't find that the port is relinquished after a few
seconds then some other resource is
holding on to it.

netstat should tell you.

My answer is not direct;that is deliberate.

Do your homework.

You have done a great deal of tech stuff I know. Hopefully this tip will help.

Best of luck.

I want every luggie to aspire to be like Narendra. Do your homework
with clear focus
 and share your results before asking something in forums.

Best,
Girish


-- 
G3 Tech
Networking appliance company
web: http://g3tech.in ?mail: girish at g3tech.in

Other related posts: