For a client server program, what is the best appr

2019-04-14 00:55发布

The program is a client server socket application being developed with C on Linux. There is a remote server to which each client connects and logs itself as being online. There will be most likely be several clients online at any given point of time, all trying to connect to the server to log themselves as being online/busy/idle etc. So how can the server handle these concurrent requests. What's a good design approach (Forking/multithreading for each connection request maybe?)?

5条回答
倾城 Initia
2楼-- · 2019-04-14 01:14

Take a look at Comer's "Internetworking with TCP/IP" volume 3 (BSD sockets version), it has detailed examples for different ways of writing servers and clients. The full code (sans explanations, unfortunally) is on the web. Or rummage around in http://tldp.org, there you'll find a collection of tutorials.

查看更多
\"骚年 ilove
3楼-- · 2019-04-14 01:15

select or poll or epoll

These are facilities on *nix systems to aggregate multiple event sources (connections) into a single waiting point. The server adds the connections to a data structure, and then waits by calling select etc. It gets woken up when stuff happens on any of these connections, figures out which one, handles it, and then goes back to sleep. See manual for details.

There are several higher level libraries built on top of these mechanisms, that make programming them somewhat easier e.g. libevent, libev etc.

查看更多
等我变得足够好
4楼-- · 2019-04-14 01:20

personally i would use the event driven approach for servers. there you register a callback that is called as soon as a connection arrives. and event callbacks whenever the socket is ready to read or write.

with a huge amount of connections you will have a great performance and resource benefit compared to threads. But i would also prefere this for a smaler count of connections.

i only would use threads if you really need to use multiple cores or if you have some request that could take longer to process and where it is too complicate to handle it without threads.

i use libev as base library to handle event driven networking.

查看更多
甜甜的少女心
5楼-- · 2019-04-14 01:21

Best approach is a combination of event driven model with multithreaded model.

You create a bunch of nonblocking sockets, but threads count should be much fewver. I.e. 10 sockets per thread.

Then you just listen for an event (incoming request) on every thread in a non-blocking mode and process it as it happens.

This technique usually performs better then non-blocking sockets or multithreaded model separately.

查看更多
放荡不羁爱自由
6楼-- · 2019-04-14 01:35

Generally speaking, you want a thread pool to service requests.

A typical structure will start with a single thread that does nothing but queue up incoming requests. Since it doesn't do very much, it's typically pretty easy for one thread to keep up with the maximum speed of the network.

That puts the items into some sort of concurrent queue. Then you have a pool of other threads reading items from the queue, doing what's needed, then depositing the result in another queue (and repeating, and repeating until the servers shuts down).

Finally, you have another single thread that just takes items from the result queue, and sends replies out to the clients.

查看更多
登录 后发表回答