对于客户端服务器程序,什么是并行接收多个客户端连接请求的最佳方法? [英] For a client server program, what is the best approach to receive multiple client connection requests in parallel?

查看:114
本文介绍了对于客户端服务器程序,什么是并行接收多个客户端连接请求的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序是在Linux上使用C开发的客户端服务器套接字应用程序。有一个远程服务器,每个客户端连接到它,并将其自身记录为在线。在任何给定时间点,很可能有几个客户端在线,所有尝试连接到服务器以将自己记录为在线/忙/空闲等。因此,服务器如何处理这些并发请求。什么是好的设计方法(对于每个连接请求的分叉/多线程可能?)

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?)?

推荐答案

服务器。在那里你注册一个回调,一旦连接到达就被调用。和事件回调每当套接字准备好读或写。与你的线程相比,你将拥有巨大的性能和资源的好处与大量的连接。

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.

我使用 libev 作为基本库来处理事件驱动的网络。

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

这篇关于对于客户端服务器程序,什么是并行接收多个客户端连接请求的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆