All the proposed design options are not very object oriented, and they are all oriented more to C than to C ++. If your work allows you to use boost, then the Boost.Asio library is fantastic for creating simple (and complex) socket servers. You can take almost any of your examples and trivially expand it to allow only 2 active connections, closing all others as soon as they are open.
At the top of my head, their simple HTTP server can be changed to do this by storing the static counter in the connection class (including the constructor, dec in the destructor), and when a new one is created, check to count and decide whether to close the connection. The connection class can also get boost :: asio :: deadline_timer to track timeouts.
This is most similar to your first design choice, boost can do it in 1 thread, and in the background something like select()
(usually epoll()
). But this is the βC ++ pathβ, and in my opinion, using select()
and raw pthread
is way C.
source share