> 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)
"Remy Lebeau" wrote:
> .
>
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...
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.
~~~~