Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Winsock socket handle reallocation

22 views
Skip to first unread message

Mubashir Khan

unread,
Dec 14, 2009, 5:12:01 AM12/14/09
to

We are facing an issue with the winsock socket handle reallocation. we have a
client server application written in c++ using winsock. It is a multithreaded
application. assume that one thread is performing a read on some socket with
handle 1234 and the remote socket is closed thus we receive 0 from recv
function. now we should try to close the socket using closesocket function.
but at the same instance, our listener thread accepts a socket connection
with socket handle 1234. Now if we call closesocket on the first thread, the
second socket is closed which is incorrect. Kindly could anyone tell us what
we are doing wrong. Is there any api to handle this case. Any help would be
appreciated.

Remy Lebeau

unread,
Dec 14, 2009, 2:04:52 PM12/14/09
to

"Mubashir Khan" <Mubash...@discussions.microsoft.com> wrote in message news:3DB7EFD2-3913-4CC3...@microsoft.com...

> assume that one thread is performing a read on some socket with handle
> 1234 and the remote socket is closed thus we receive 0 from recv function.
> now we should try to close the socket using closesocket function. but at
> the same instance, our listener thread accepts a socket connection with
> socket handle 1234.

accept() cannot, and will not, return a new socket handle that has the same value as an existing open socket handle. The only way that can happen is if you actually closed the original socket first, allowing winsock to reuse it (just like the rest of the OS reuses various other types of handles).

> Now if we call closesocket on the first thread, the second socket is
> closed which is incorrect.

The only way that could happen is if your code is calling closesocket() twice on the same socket handle, which would be a bug on your part, not on WinSock's.

--
Remy Lebeau (TeamB)

Mubashir Khan

unread,
Dec 23, 2009, 12:32:01 AM12/23/09
to
i noticed that we are using while(!closesocket(s)) Sleep(200);
this may be incorrect. what is the best way to call closesocket.
thanks
Mubashir

"Remy Lebeau" wrote:

> .
>

SonicBison

unread,
Dec 31, 2009, 4:22:02 PM12/31/09
to
From the documentation "An application should always have a matching
call to closesocket for each successful call to socket to return any
socket resources to the system."

On Dec 23, 12:32 am, Mubashir Khan


<MubashirK...@discussions.microsoft.com> wrote:
> i noticed that we are using while(!closesocket(s)) Sleep(200);
> this may be incorrect. what is the best way to call closesocket.
> thanks
> Mubashir
>
> "Remy Lebeau" wrote:
>

> > "Mubashir Khan" <MubashirK...@discussions.microsoft.com> wrote in messagenews:3DB7EFD2-3913-4CC3...@microsoft.com...

Alun Jones

unread,
Jan 25, 2010, 7:14:31 PM1/25/10
to

"Mubashir Khan" <Mubash...@discussions.microsoft.com> wrote in message
news:1262E1C6-5532-41CF...@microsoft.com...

> i noticed that we are using while(!closesocket(s)) Sleep(200);
> this may be incorrect. what is the best way to call closesocket.

Call closesocket() once, and once only, for each socket handle that gets
created (whether that's through a call to socket, WSASocket, accept, etc).

If you are concerned about watching the FIN/FIN-ACK/ACK exchange, use
shutdown() to send the FIN, and wait for FD_CLOSE or a time span before
calling closesocket(). Otherwise, just call closesocket() and let Windows
take care of it.

Calling closesocket() in a loop will do the sort of nasty behaviour you're
seeing.

Alun.
~~~~

0 new messages