Unix Programming - Unable to reuse old port for listening

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > Unable to reuse old port for listening





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Unable to reuse old port for listening
jamx

2006-03-16, 7:51 am

Even when using the getsockopt() function with SO_REUSEADDR
socketoption, i still get the message: Error: connections.c:
conn_listen: bind (Address already in use)
How can i fix this problem?

a part of the code:
/* create us a socket.. */
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

/* ..and set it to non-blocking */
if (-1 == (flags = fcntl(s, F_GETFL, 0))) flags = 0;

if (-1 == (fcntl(s, F_SETFL, flags | O_NONBLOCK)))
{
log(LOG_ERROR, "connections.c: conn_listen: fcntl");
return NULL;
}

bzero(&sin, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);

/* If there is some old socket crap hanging in kernel, this
avoid the
"Address already in use" error while using bind()
But u also gotta be careful not to use conn_listen on a
socket used somewhere else ;-p */
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(int)) == -1) {
log(LOG_ERROR, "connections.c: conn_listen:
setsockopt");
return NULL;
}

if (bind(s, (struct sockaddr *)&sin, sizeof(struct
sockaddr_in)) == -1)
{
log(LOG_ERROR, "connections.c: conn_listen: bind");
return NULL;
}

netstat output:
Proto Recv-Q Send-Q Local Address Foreign Address
State
tcp 0 0 *:22000 *:*
LISTEN

Rainer Temme

2006-03-16, 7:51 am

jamx wrote:
> Even when using the getsockopt() function with SO_REUSEADDR
> socketoption, i still get the message: Error: connections.c:
> conn_listen: bind (Address already in use)
> How can i fix this problem?


....


> netstat output:
> Proto Recv-Q Send-Q Local Address Foreign Address
> State
> tcp 0 0 *:22000 *:*
> LISTEN



Hi jamx,

you cannot "fix" this problem.

Port 22000 is in use ... even REUSEADDR will _not_ allow
you to bind to a port that is bound an listened to by another
process.

REUSEADDR does _only_ help when the portnumber appears only
with connections in TIME_WAIT state.

Rainer
phil-news-nospam@ipal.net

2006-03-17, 2:57 am

On Thu, 16 Mar 2006 13:10:30 +0100 Rainer Temme <Rainer.Temme@nospam.siemens.com> wrote:
| jamx wrote:
|> Even when using the getsockopt() function with SO_REUSEADDR
|> socketoption, i still get the message: Error: connections.c:
|> conn_listen: bind (Address already in use)
|> How can i fix this problem?
|
| ...
|
|
|> netstat output:
|> Proto Recv-Q Send-Q Local Address Foreign Address
|> State
|> tcp 0 0 *:22000 *:*
|> LISTEN
|
|
| Hi jamx,
|
| you cannot "fix" this problem.
|
| Port 22000 is in use ... even REUSEADDR will _not_ allow
| you to bind to a port that is bound an listened to by another
| process.
|
| REUSEADDR does _only_ help when the portnumber appears only
| with connections in TIME_WAIT state.

So what is the usefulness of NOT using SO_REUSEADDR?

Maybe it should have been SO_REUSEADDR by defualt and create a symbol
named SO_NOREUSEADDR instead. It sure would save a lot of grief on
the part of many people, including system administrators that had to
deal with programmers that didn't know to use SO_REUSEADDR.

--
-----------------------------------------------------------------------------
| Phil Howard KA9WGN | http://linuxhomepage.com/ http://ham.org/ |
| (first name) at ipal.net | http://phil.ipal.org/ http://ka9wgn.ham.org/ |
-----------------------------------------------------------------------------
Frank Cusack

2006-03-17, 2:57 am

On 17 Mar 2006 03:41:14 GMT phil-news-nospam@ipal.net wrote:
> On Thu, 16 Mar 2006 13:10:30 +0100 Rainer Temme <Rainer.Temme@nospam.siemens.com> wrote:
> | Port 22000 is in use ... even REUSEADDR will _not_ allow
> | you to bind to a port that is bound an listened to by another
> | process.
> |
> | REUSEADDR does _only_ help when the portnumber appears only
> | with connections in TIME_WAIT state.
>
> So what is the usefulness of NOT using SO_REUSEADDR?
>
> Maybe it should have been SO_REUSEADDR by defualt and create a symbol
> named SO_NOREUSEADDR instead. It sure would save a lot of grief on
> the part of many people, including system administrators that had to
> deal with programmers that didn't know to use SO_REUSEADDR.


The TIME_WAIT state is to "guarantee" that a delayed packet does not
make its way into a different (newer) connection. SO_REUSEADDR breaks
that guarantee.

-frank
Barry Margolin

2006-03-17, 2:57 am

In article <dvdb4q0d76@news4.newsguy.com>, phil-news-nospam@ipal.net
wrote:

> So what is the usefulness of NOT using SO_REUSEADDR?


Safety. At the time you call bind(), there's no way to know whether
you're planning on listening or connecting the socket, i.e. whether it
will be a server or client. To prevent you from inadvertently creating
a conflict, it defaults to NOT allowing reuse, and requires a positive
statement to allow it.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Frank Cusack

2006-03-21, 3:17 am

On Fri, 17 Mar 2006 01:42:24 -0500 Barry Margolin <barmar@alum.mit.edu> wrote:
> In article <dvdb4q0d76@news4.newsguy.com>, phil-news-nospam@ipal.net
> wrote:
>
>
> Safety. At the time you call bind(), there's no way to know whether
> you're planning on listening or connecting the socket, i.e. whether it
> will be a server or client. To prevent you from inadvertently creating
> a conflict, it defaults to NOT allowing reuse, and requires a positive
> statement to allow it.


That has nothing to do with it. See my earlier post.

-frank
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com