Polling over thousands of TCP sockets

≯℡__Kan透↙ 提交于 2019-12-07 06:19:48

问题


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#.

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

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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10300428/polling-over-thousands-of-tcp-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!