Conditional Accept using a blocking WSAAccept call issues RRS feed

  • Question

  • We are trying to do a conditional accept using a blocking call to WSAAccept which is causing some problems. 

    The problem is that once we call Close on the socket there is a chance that the socket handle is reused before we can stop the accepting thread from calling WSAAccept with the old handle.
    Also we cannot wait for the listening thread to exit the accepting loop as it could be blocked in WSAAccept and we don't want to wait for a new connection to be accepted before we stop listening.

    There are probably many things that would fix the issue, a few that come to mind are..

    1) Cancel a blocking WSAAccept call without closing the socket. (Not possible from what I have seen elsewhere)
    2) Not using a blocking WSAAccept call. In which case is it possible to do a conditional accept without using a blocking call?
    3) Finding out if the WSAAccept call is blocked waiting for a connection. If it is then we know it is safe to Close the socket.

    We have noticed in the MSDN documentation for WSAAccept that it is possible to queue an APC on the accepting thread to determine if it is in an alertable state (and therefore not about to call WSAAccept) but this sounds like an ugly way to approach this problem

    If anyone has an answer for these points or has any different ideas for the conditional accept then we would appreciate their input.

    Wednesday, March 5, 2014 12:54 PM