I am planning on implementing a special purpose TCP server in C on Linux. After doing a little research, it looks like there are a few ways to do this, including single threaded, one thread per connection, and others. For the sockets, there are options like datagram vs stream, and blocking vs non-blocking.
Most of the communication is going to look like:
Client: request id [request info]
Server: status id [response info]
or
Client: request id [request info]
Server: status id [response info]
Client: additional request id [request info]
Server: status id [response info]
Where everything is <1kB and most things are <512B. There may be many individual requests in a short time, however.
So, how do I set up the server so it works most effectively (ie, doesn’t hog resources, doesn’t deny client requests)?
I think your question boils down to performance. Does it?
If so, three questions:
Anyway, I’d start simple. Make it non-blocking, and single threaded. Profile and stress-test it. Then, if you’re not happy with the presented performance, identify the root cause and try to fix it. As ultimate option, scale it to one worker thread per processor core.
If you don’t care about reliability and order of delivery, you might want to use UDP.