user4582812
user4582812

Reputation: 621

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()?

Upvotes: 5

Views: 3383

Answers (2)

Len Holgate
Len Holgate

Reputation: 21644

Why do you want to use a blocking call?

If you're using I/O Completion Ports then the best way to handle connection establishment is to use AcceptEx() and not wait for data along with the connect. The reason for this is that using AcceptEx() means that you don't need a separate thread to deal with connection establishment (i.e. a normal "accept loop") which reduces unnecessary context switches.

The 'accept and read data' option of AcceptEx() can open you up to a denial of service attack if connections connect and do not send data, and it's hard to guard against it unless you add a housekeeping thread which defeats the purpose of using AcceptEx() in the first place...

Upvotes: 1

Andy Brown
Andy Brown

Reputation: 13009

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.

Upvotes: 3

Related Questions