轮询数千TCP套接字 [英] Polling over thousands of TCP sockets

查看:259
本文介绍了轮询数千TCP套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要连接到几千通过TCP客户端上的一个专有协议来周期性地采集数据。我需要写在C#.NET服务器应用程序。

I need to connect to thousands of clients over TCP on a proprietary protocol to acquire data cyclically. I need to write a .NET server application in C#.

第一个尝试是为每个TCP套接字的自己的线程,这工作,但需要大量的CPU使用率

The first attempt was to create for each tcp socket an own thread, which works but needs a lot of cpu usage.

我发现,这将是一个更好的主意,使用.NET线程池来代替。据我了解( http://msdn.microsoft.com/en-us /library/ms973903.aspx )我可以使用定时器,以获得每个插座周期性收购某一时期内(如1秒)的数据。这不适合我,因为插座超时一旦连接是openende因为有很多插座哪些必须被打开之前它是开放式插座再次打开的。

I found out that it would be a better idea to use the .NET threadpool instead. As far as I understand (http://msdn.microsoft.com/en-us/library/ms973903.aspx) I could use timers in order to get each socket acquire the data cyclically in a given period (like 1 sec). This does not work for me because the sockets time out once the connection was openende because there are a lot of more sockets which have to be opened before it's the open sockets turn again.

另一次尝试使用异步回调。这为我工作,但我不知道怎么去插座采集数据的周期性???

Another try was using asynchronous callbacks. This would work for me but I don't know how to get the sockets acquire data cyclically???

推荐答案

使用try插座的高性能的API 的,其同时允许接收在一个非常大的数量的插座的数据,而无需使用一个线程每个插槽。在文章的底部,有一个完整的示例的链接。还有MSDN文章为的SocketAsyncEventArgs类一个样本。

Try using Socket's high performance API which allows simultaneously receiving data on a very large number of sockets, without using one thread per socket. At the bottom of the article there's a link to a complete sample. There's also a sample in the MSDN article for the SocketAsyncEventArgs class.

这篇关于轮询数千TCP套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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