|
Home > Archive > Unix Programming > June 2006 > EADDRINUSE when it shouldnt be... can sockaddr be reused?
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 |
EADDRINUSE when it shouldnt be... can sockaddr be reused?
|
|
| chengiz@my-deja.com 2006-06-21, 1:22 pm |
| Hi,
We are getting EADDRINUSE from bind even though the previous sockets
have closed and SO_REUSEADDR is set. It may or may not be important
that the connecting sockets are run from a thread that is spawned by
the server like so:
1. Server spawns thread, then waits in accept.
2. Thread (client) tries to connect. Server accepts the connection, and
the listening socket is closed. The sockets talk and both are closed
down.
3. Only after this does the server try to bind another socket, and 2-3
are supposed to repeat.
However, 2 works only the first time, and 3 fails with EADDRINUSE. The
server uses the same sockaddr object every time (could that be a
problem?) and so does the client, however the two sockaddrs are
different. Any help with this will be greatly appreciated.
Khan
| |
| Barry Margolin 2006-06-21, 7:22 pm |
| In article <1150904123.708665.313800@m73g2000cwd.googlegroups.com>,
chengiz@my-deja.com wrote:
> Hi,
>
> We are getting EADDRINUSE from bind even though the previous sockets
> have closed and SO_REUSEADDR is set. It may or may not be important
> that the connecting sockets are run from a thread that is spawned by
> the server like so:
> 1. Server spawns thread, then waits in accept.
> 2. Thread (client) tries to connect. Server accepts the connection, and
> the listening socket is closed. The sockets talk and both are closed
> down.
> 3. Only after this does the server try to bind another socket, and 2-3
> are supposed to repeat.
>
> However, 2 works only the first time, and 3 fails with EADDRINUSE. The
> server uses the same sockaddr object every time (could that be a
> problem?) and so does the client, however the two sockaddrs are
> different. Any help with this will be greatly appreciated.
Is there any process forking taking place? Maybe a child process
inherited the listening socket before it was closed.
I suggest you use "lsof" or "netstat -anp" to see what processes have
sockets referencing the port.
--
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 ***
| |
| chengiz@my-deja.com 2006-06-22, 1:27 pm |
| Barry Margolin wrote:
> Is there any process forking taking place? Maybe a child process
> inherited the listening socket before it was closed.
>
> I suggest you use "lsof" or "netstat -anp" to see what processes have
> sockets referencing the port.
Yes, this was exactly what was happening... there was a system() call.
Using fcntl with FD_CLOEXEC on the listening socket ie. fcntl(fd,
F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) seems to have solved the
problem. Thanks for the help!
Khan
|
|
|
|
|