I have a server program (works on all three major os systems), created in c++ which strives to connect two strangers for communication. My current model seems depreciated, and I am simply wondering if there is a better way to go about serving the clients.

-server receives connection request
-checks for ban
-starts thread for this socket


[the thread simply loops through these steps]

-confirm connection with partner (check manager)
-if unconnected request partner from manager class
-receive on my socket <- client sends keep alive packets every 2s or so
-on disconnect inform manager, and close thread.


[the manager class works like this]

-add socket: push_back on my vector of sockPairs
-request partner: find unconnected sockPair, if none exist create new sockPair, once connected, mark sockPair for removal, if already marked, remove it.
-remove socket: add flag to manager id of partner that I've disconnected
-check socket: check manager id for disconnection


I'm thinking a map would be much more efficient, however I'm not positive, as I've never worked with them, what else do you think I should change? I'm hoping to be able to serve 200 clients with this model, I'm really unsure if the current model could handle it...

Accepted Answer

A thread per connection simply does not scale. You need to use a mechanism such as select(), poll(), epoll(), WSAAsyncSelect() or anything else that will let you query a set of sockets for events. Then you process each socket in order, and repeat.

Written by Erik
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki