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连接到数千个客户端,以周期性地获取数据。我需要用c#编写. net服务器应用程序。
The first attempt was to create for each tcp socket an own thread, which works but needs a lot of cpu usage.
第一个尝试是为每个tcp套接字创建一个自己的线程,它可以工作,但是需要大量的cpu使用。
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.
我发现用。net threadpool代替是一个更好的主意。就我所知(http://msdn.microsoft.com/en-us/library/ms973903.aspx),我可以使用计时器来让每个套接字在给定的周期内(比如1秒)周期性地获取数据。这对我来说不起作用,因为一旦连接是openende,套接字就会超时,因为还有很多套接字必须在打开套接字之前打开。
Another try was using asynchronous callbacks. This would work for me but I don't know how to get the sockets acquire data cyclically???
另一个尝试是使用异步回调。这对我来说是可行的,但我不知道如何让sockets周期性地获得数据?
2 个解决方案
#1
6
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.
尝试使用Socket的高性能API,它允许同时接收大量Socket上的数据,而不是每个Socket使用一个线程。在本文的末尾有一个到完整示例的链接。在MSDN文章中还有一个示例,用于SocketAsyncEventArgs类。
#2
0
Why not populate a queue with the addresses you need to poll and have your thread pool take items off the queue to process?
为什么不将需要轮询的地址填充到队列中,并让线程池从队列中取出要处理的项呢?
Once you are done with an item push it to the back of the queue.
完成一个项目后,将它推到队列的后面。
#1
6
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.
尝试使用Socket的高性能API,它允许同时接收大量Socket上的数据,而不是每个Socket使用一个线程。在本文的末尾有一个到完整示例的链接。在MSDN文章中还有一个示例,用于SocketAsyncEventArgs类。
#2
0
Why not populate a queue with the addresses you need to poll and have your thread pool take items off the queue to process?
为什么不将需要轮询的地址填充到队列中,并让线程池从队列中取出要处理的项呢?
Once you are done with an item push it to the back of the queue.
完成一个项目后,将它推到队列的后面。