Unable to reuse old port for listening
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Unable to reuse old port for listening




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Unable to reuse old port for listening  
jamx


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 12:51 PM

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






[ Post a follow-up to this message ]



    Re: Unable to reuse old port for listening  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 12:51 PM

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





[ Post a follow-up to this message ]



    Re: Unable to reuse old port for listening  
phil-news-nospam@ipal.net


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-06 07: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/ 
|
----------------------------------------------------------------------------
-





[ Post a follow-up to this message ]



    Re: Unable to reuse old port for listening  
Frank Cusack


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-06 07: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.sieme
ns.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





[ Post a follow-up to this message ]



    Re: Unable to reuse old port for listening  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-06 07: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 ***





[ Post a follow-up to this message ]



    Re: Unable to reuse old port for listening  
Frank Cusack


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-21-06 08: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:47 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register