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?
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 forconnect
. (note that this also automagically makes your socket non-blocking, which requires another call toWSAAsyncSelect
and a call toioctlsocket
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 onWaitForMultipleObjects
). This will also make your socket non-blocking, which you can undo similarly to what is done forWSAAsyncSelect
.(As a complete side note, you can increase the number of sockets handled by
select
by definingFD_SETSIZE
before includingwinsock2.h
)