Search code examples
c++socketswinapinetwork-programmingoverlapped-io

Should I use AcceptEx() or WSAAccept()?


I am using Overlapped IO, I want to accept client connections using a blocking call, the same way I do with a normal accept(). I am not sure but I think that AcceptEx() does not block, while WSAAccept() does. So is WSAAccept() similar to accept()?


Solution

  • accept() and WSAAccept() will both block unless you've used ioctlsocket to set the listener to non-blocking mode. So you could use either of those to accept a client while blocking.

    However you'll gain more control if you use WSAEventSelect to register an event against FD_ACCEPT on your listener. Your event will be set when a client is ready to be accepted without blocking.

    You could then combine this event with, say, a timeout or another event that you can signal if you want to cancel the listen (e.g. on application exit) in a call to WaitForMultipleObjectsEx.