Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listening to multiple sockets: select vs. multi-threading

A server needs to listen to incoming data from several sockets (10-20). After some initializations, those sockets are created and do not change (i.e. no new sockets accepted, and none of them is expected to close during the lifetime of the server).

One option is to select() on all sockets, then deal with incoming data per socket (i.e. route to proper handling function).

Another option is to open one thread per socket and let each thread recv() and handle the input.

(The first option has the benefit of setting a timeout, but this is not an issue in this case, since all the sockets are quite active).

Assuming the following: Windows server, has enough memory such that 20MB (for the 20 threads) is a non-issue, is any of those options expected to be faster then the other?

like image 651
Paul Oyster Avatar asked Jun 04 '26 07:06

Paul Oyster


1 Answers

There's not much in it in you app. Typically, using a thread-per-socket is easier than asynchronous approaches because it's a simpler overall structure and it's easier to maintain state.

like image 179
Martin James Avatar answered Jun 05 '26 23:06

Martin James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!