有效地管理多个TCP连接 [英] Manage many TCP connections effectively and efficiently

查看:79
本文介绍了有效地管理多个TCP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,如果有人可以给我就如何有效地管理大量的TCP连接的任何意见或建议。我说的1000左右的TCP连接(也许更多)。应用管理所有这些连接需要定期拉从客户端(在连接的另一端)的信息。例如,可能每30秒。我将使用.NET 4.0这一点。有什么内置在帮助这还是一种特殊的方式来组织一切管理所有这些连接,而不使这个应用程序洙越陷越深,这是没有用的?

I am curious if someone can give me any ideas or suggestions on how to manage a lot of TCP connections efficiently. I am talking around 1000 tcp connections (maybe more). The application managing all these connections needs to pull information from the client (other end of the connection) periodically. For example, maybe every 30 seconds. I would be using .NET 4.0 for this. Is there anything built in to help with this or a special way to structure everything to manage all these connections without making this application be soo bogged down that it is not useful?

推荐答案

我可以给你深入了解一个快速的位:

I can give you one quick bit of insight:

甚至不要去想产卵每个连接一个线程。使用该框架内置的异步/任务的框架,而不是来管理连接。那是;让线程池做到这一点。

Do not even think about spawning a thread per-connection. Use the framework's built-in async/task frameworks instead to manage the connections. That is; let the thread pool do it.

另外,要小心密切关注你的线程在做什么;要确保你永远不能被读取/从多个线程写一个连接的流一次。但在同一时间,小心您如何使用锁定机制这样做,这样你就不会结束与1000活动连接一个过程,一个线程池满等待获取锁,他们将永远不会线程。

Also, be careful to watch closely what your threads are doing; Be sure you can't ever be reading/writing a connection's stream from multiple threads at once. But at the same time, be careful with how you use locking mechanisms to do so, so you don't end up with a process with 1000 active connections, and a thread pool full of threads waiting to acquire locks that they will never get.

这篇关于有效地管理多个TCP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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