在ac#应用程序中维护大量打开的套接字
我正在用 C# 编写一个应用程序,我想在其中维护大量打开的套接字连接 (tcp),并能够响应来自所有连接的数据。现在,正常的做法(据我所知)是对所有这些线程调用 BeginRead,但据我所知,BeginRead 会生成一个线程并坐在该线程中等待数据,而且我已经读到线程不会当使用大量它们时,往往可以很好地扩展。虽然我可能是错的,如果是这样的话请告诉我。
这可能听起来很愚蠢,但我想要的是能够维护尽可能多的开放套接字连接(在我的机器/操作系统允许的情况下),同时仍然能够对来自其中任何一个的数据尽快做出反应可能,并使用尽可能少的系统资源来执行此操作。
我想到的是将它们全部放入某种列表中,然后在它们上循环运行一个线程,检查新数据并对新数据进行操作(如果有的话),尽管我认为这样的循环最终会烧毁我的CPU(因为循环中没有任何东西会阻塞)。有什么方法(简单与否,我不介意使用一些更复杂的算法来解决这个问题)可以实现这一目标?任何帮助将不胜感激。
I'm writing an application in C# where I want to maintain a large number of open socket-connections (tcp), and be able to respond to data that comes in from all of them. Now, the normal thing to do (afaik) would be to call BeginRead on all of them, but as far as I know BeginRead spawns a thread and sits and waits for data in that thread, and I've read that threads don't tend to scale very well when using a lot of them. Though I might be wrong about this, if that's the case please just tell me.
This will probably sound idiotic, but what I want is to be able to maintain as many open socket-connections as possible (as my machine/os allows), while still being able to react to data coming in from any of them as fast as possible, and using as little system-resources as possible doing this.
What I've thought about is to put all of them in some kind of list, and then run a single thread in loop over them checking for new data and acting on that new data (if there is any), though I'd think that a loop like that will end up frying my cpu (cause there is nothing in the loop that blocks). Is there any way (simple or not, I don't mind getting into some more complex algorithms to solve this) that I can achieve this? Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。异步方法不会生成线程,它们使用 IO 完成端口。
BeginXXX
方法已被弃用。请改用 XxxxAsync 方法。No. The asynchronous methods do not spawn threads, they use IO Completion Ports.
The
BeginXXX
methods have been deprecated. Use theXxxxAsync
methods instead.