更好的客户端/服务器套接字系统解决方案
我有一个服务器和客户端(1000),服务器每秒进行通信。我正在服务器上为每个客户端创建一个线程。
我想知道的是:
为每个客户端使用线程(线程在成功登录时创建)是一个好主意还是有其他方法可以提高服务器和客户端之间的通信效率?
服务器采用 Java 编程,客户端采用 C 语言
和服务器规格:
64 位 TwelveCore AMD CPU 2xAMD Opteron™ 6174,2,2
Ghz DDR3-RAM bis 128 GB ECC,reg。 (错误已更正)
20 TB S-ATA II 和 6 TB SAS(热插拔)硬盘
谢谢
I have a Server and clients(1000) which the server communicates in every sec. I am creating a single thread on the server for each Client.
What I want to know:
Is it a good idea to use Thread for each client (Thread is created, when they loggedin succesfully) or is there another way to make more efficient to communicate between server and clients?
Server programmed with Java and the clients with C
And server specifications:
64Bit TwelveCore AMD CPU 2xAMD Opteron™ 6174,
2,2 Ghz DDR3-RAM bis 128 GB ECC, reg. (errorCorrected)
20 TB S-ATA II and 6 TB SAS (HotSwap) HDD
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
理论上,您应该对每个客户端的请求进行单独的线程处理。
然而实际上,由于硬件资源有限,这是不可能的。硬件资源的限制阻止了创建无限数量的线程,并且就操作系统要执行的管理这些线程的任务而言,为每个请求创建单独的线程是一项成本高昂的操作。
要解决这个问题,您应该考虑使用线程池,通过它您可以重用线程来服务不同的客户。如果您在服务器端使用 Java,则 concurrency 包(1.5 及以上)提供了创建和使用线程池的完整基础设施。参见 http://download.oracle.com/javase/tutorial/essential/concurrency/pools .html 了解更多详细信息。
Theoretically you should be having separate thread processing for each client's request.
However practically this is not possible giving the constraint of limited hardware resources. Limit on hardware resources stops from creating unlimited number of threads and creating a separate thread for each request is a costly operation in terms of tasks to be performed by OS to manage those threads.
To fix the problem you should think towards having thread-pool by which you can reuse the threads to serve different clients. If you use Java on server side then concurrency package of Java (1.5 onwards) provides the full infrastructure to create and use thread-pools.See http://download.oracle.com/javase/tutorial/essential/concurrency/pools.html for more details.
您可以使用非阻塞 NIO 来共享线程之间的连接。最小数量为 1 ;) 如果您想使用 Dispacher 模型,我建议使用一个为您处理详细信息的库。例如网蒂。
在我所做的测试中,使用 10,000 个连接会浪费大约 1 个核心。如果您有 1,000 个核心,我预计您会浪费大约 10% 的一个核心。我的观点是,虽然它更高效,但可能不值得对您的应用程序进行重大更改来支持它。
You can use non-blocking NIO to share connections between threads. The minimum number being 1 ;) If you want to use a Dispacher model, I suggest using a library which handles the details for you. e.g. netty.
In tests I have done, using 10,000 connections wastes about 1 core. If you have 1,000 I would expect you are wasting about 10% of one core. My point being that while its more efficient, it may not be worth signifciant changes to your application to support it.
我建议您研究一下 Java NIO(新 I/O),您可以使用它来使您的服务器成为单线程(至少很少线程)。
这里有一些很好的例子:
I would suggest you look into Java NIO (New I/O) which you can use to make your server single threaded (at least very few threads).
Here are some nice examples: