Non-blocking socket connect on Windows without ConnectEx

888 views Asked by At

I need to initiate 1000's of client connections in a single process, the key limitation I need to work around is the driver does not support ConnectEx, so I cannot have a pure IOCP solution.

My first thought was a thread pool to handle connections, where each handle can handle up to 64 connections using plain connection/select semantics, and once connected continue with IOCP. But this cannot work; once select is running I can't add another socket to the FD_SET. So I would have to set the sockets to non-blocking and poll them instead.
The best solution may be the simplest; one connecting client per thread. Assuming I can keep the connection rate reasonable the number of threads in the pool could be small.

It is an odd situation, ideally the driver would support ConnectEx but it doesn't (for now) and I need to work around it in the best way possible.

Is there another way?

1

There are 1 answers

2
Hasturkun On BEST ANSWER

First, it's possible to break select using a dummy socket (eg. a UDP socket connected to itself), though that might be costly in your case.

If you have a window that can handle messages around, you can use WSAAsyncSelect to get asynchronous notifications for connect. (note that this also automagically makes your socket non-blocking, which requires another call to WSAAsyncSelect and a call to ioctlsocket to disable).

Lacking that, you can use WSAEventSelect, though you'll find that this limits you to 64 sockets per wait (as that's the upper limit on WaitForMultipleObjects). This will also make your socket non-blocking, which you can undo similarly to what is done for WSAAsyncSelect.

(As a complete side note, you can increase the number of sockets handled by select by defining FD_SETSIZE before including winsock2.h)