Winsock2 receiving data from multiple clients without iterating over each client

Viewed 28

I use winsock2 with C++ to connect thousands of clients to my server using the TCP protocol. The clients will rarely send packets to the server.... there is about 1 minute between 2 packets from a client.

Right now I iterate over each client with non-blocking sockets to check if the client has sent anything to the server.

But I think a much better design of the server would be to wait until it receives a packet from the thousands of clients and then ask for the client's socket. That would be better because the server wouldn't have to iterate over each client, where 99.99% of the time nothing happens. And with a million clients, a complete iteration might take seconds....

Is there a way to receive data from each client without iterating over each client's socket?

1 Answers

What you want is to use IO completion ports, I think. See https://docs.microsoft.com/en-us/windows/win32/fileio/i-o-completion-ports. Microsoft even has an example at GitHub for this: https://github.com/microsoft/Windows-classic-samples/blob/main/Samples/Win7Samples/netds/winsock/iocp/server/IocpServer.Cpp

BTW, do not expect to server million connections on single machine. Generally, there are limits, like available memory, both user space and kernel space, handle limits, etc. If you are careful, you can probably serve tens of thousands of connection per process.

Related