FatalCatharsis
FatalCatharsis

Reputation: 3567

in winsock, how would i stop a accept function from blocking from another thread?

so here's my situation. I'm working with winsock, learning some things about networking and protocol devlopment, and i've reached an impasse. I'm right now doing a server that blocks at an accept call, but has another thread in the background waiting for user input and handling user input. this user input is used for commands like send,ping, and say, etc. but now i would like a command called exit that will shut down winsock, release all open connections, and then end the program. problem is, this accept thread is still blocking.

so here's the questions. 1) is there anyway to stop a winsock function from blocking while it's called, and 2) is something like this in non-blocking mode:

while(m_continue)
{
      Sleep(1);
      tempsock = accept(m_listenSock, NULL, NULL);
      if(tempsock != INVALID_SOCKET)
      {
            if(WSAGetLastError() == WSAEWOULDBLOCK)
                  continue;
            else 
                  return 1;
      }
      break;
{

superfluous or just too inneficient to use?

Upvotes: 4

Views: 1946

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 992767

From the user command thread, when you handle the "exit" command, close the m_listenSock socket. The blocking accept() call will immediately drop out with INVALID_SOCKET.

Be sure not to close the m_listenSock handle twice.

Upvotes: 3

Related Questions