Closed sockets remains in CLOSE WAIT for long time

140 views
Skip to first unread message

sridhar

unread,
Jun 5, 2010, 7:48:35 AM6/5/10
to
Hi,

In my application, some sockets remain in close wait state for long
time in server side. I checked applying debug prints and found that we
are closing the sokects using closesocket() method. The closesocket
method returns 0 after closing the socket.

But when we nestat - we still see the closed socket in the close wait
state.

We added shutdown socket method (before closesocket) still no success.
Any help regarding this is appreciated ..

Regards,
Sri.

Peter Duniho

unread,
Jun 5, 2010, 12:36:28 PM6/5/10
to
sridhar wrote:
> [...]

> We added shutdown socket method (before closesocket) still no success.
> Any help regarding this is appreciated ..

When you call shutdown(), are you actually waiting for the other end to
also call shutdown() (i.e. reading from the socket until 0 is returned),
closing the connection gracefully?

sridhar

unread,
Jun 8, 2010, 3:21:01 AM6/8/10
to
Hi Peter,

We basically set SO_LINGER to 0(off) using setsocket option, so we
don't need to wait for data to be read/write.

Still we read socket until we get 0. Code snip is below

[code]
status = shutdown(remoteSocket,SD_BOTH);
char buf[8*1024];
while (recv(remoteSocket, buf,sizeof(buf),0) > 0)
{
SleepProcess(100);
}
[/code]

Regards,
Sri

Peter Duniho

unread,
Jun 8, 2010, 3:44:57 AM6/8/10
to
sridhar wrote:
> Hi Peter,
>
> We basically set SO_LINGER to 0(off) using setsocket option, so we
> don't need to wait for data to be read/write.
>
> Still we read socket until we get 0. Code snip is below
>
> [code]
> status = shutdown(remoteSocket,SD_BOTH); [...]

Calling shutdown() with SD_BOTH basically indicates to Winsock that you
are done both sending _and_ receiving. It doesn't make any sense to
then call recv() after that.

I can't say for sure it's related to your original question, but you
ought to pass SD_SEND and then recv() whatever data the other end still
has to send. The other end can use SD_BOTH, because it's already heard
from your end that no more data is coming and so it knows it doesn't
need to recv() any more.

In general, I find that when I write my network code to follow the
established graceful closure conventions, everything works fine.
Conversely, when things aren't working, it's generally because I haven't
followed the established conventions (though never on purpose, it does
happen).

Pete

David Schwartz

unread,
Jul 26, 2010, 8:41:05 PM7/26/10
to
On Jun 5, 4:48 am, sridhar <duraisrid...@gmail.com> wrote:

> In my application, some sockets remain in close wait state for long
> time in server side. I checked applying debug prints and found that we
> are closing the sokects using closesocket() method. The closesocket
> method returns 0 after closing the socket.
>
> But when we nestat - we still see the closed socket in the close wait
> state.

Actually, you see the connection the closed socket used to refer to in
the CLOSE_WAIT state.

> We added shutdown socket method (before closesocket) still no success.
> Any help regarding this is appreciated ..

You need to close *all* sockets that reference that connection. Until
the last socket is closed, the connection (or what's left of it) will
remain around in the CLOSE_WAIT state, waiting for the last socket to
be closed.

DS

Reply all
Reply to author
Forward
0 new messages