问题描述
我的应用程序需要迅速广播消息,以大量的客户端(1000-S),并收集结果。
My application needs to rapidly broadcast a message to a large number of clients (1000-s) and collect results.
我在考虑是否要使用 BeginSend / EndSend 等功能的家庭,或者使用 SendAsync 家庭 - 有什么性能差异?他们有什么区别可言,除了*异步家庭不需要一个IAsyncResult分配?
I'm thinking whether to use the BeginSend/EndSend etc. families of functions, or to use the SendAsync family - is there any performance difference? What is their difference at all, except for the *Async family not requiring allocation of an IAsyncResult?
如果我理解正确的,但它们使用的IO完成端口和标准的.NET线程池......那么,有什么区别呢?
If I'm understanding correctly, they both use IO completion ports and the standard .net thread pool... So what's the difference?
推荐答案
所不同的只是在使用的模式。
The difference is only in the pattern used.
SendAsync
的事实使用的。 BeginSend
和 EndSend
使用的。
SendAsync
in facts use an Event-Based Pattern. BeginSend
and EndSend
use the IAsyncResult pattern.
编辑:我不知道如何使用的IAsyncResult
接口在插座实施
类,但这里是从MSDN 文件解释,当实现一个图案或另一个。
I don't know how the IAsyncResult
interface is implemented in Socket
class but here is a document from MSDN that explains when to implement one pattern or the other one.
这是最后的部分摘录:
虽然基于事件的异步模式下有许多好处 previously提到的情况下,它 确实有一些缺点,你 应该意识到,如果性能是 你最重要的要求。
有三种方案的 基于事件的模式并没有解决 以及IAsyncResult的图案:
There are three scenarios that the event-based pattern does not address as well as the IAsyncResult pattern:
-
在一个IAsyncResult的阻塞等待
Blocking wait on one IAsyncResult
在许多IAsyncResult的对象阻塞等待
Blocking wait on many IAsyncResult objects
轮询的IAsyncResult的完成
Polling for completion on the IAsyncResult
您可以通过解决这些场景 使用基于事件的模式,但 这样做比使用更麻烦 该IAsyncResult的模式。
You can address these scenarios by using the event-based pattern, but doing so is more cumbersome than using the IAsyncResult pattern.
开发人员经常使用的IAsyncResult 图案服务通常 具有非常高的性能 要求。例如,轮询 完成情况是 高性能服务器技术。
Developers often use the IAsyncResult pattern for services that typically have very high performance requirements. For example, the polling for completion scenario is a high-performance server technique.
此外,基于事件的模式 比效率较低 IAsyncResult的模式,因为它 创建更多的对象,尤其是 EventArgs的,并且因为它同步 跨线程。
Additionally, the event-based pattern is less efficient than the IAsyncResult pattern because it creates more objects, especially EventArgs, and because it synchronizes across threads.
下面的列表显示了一些 如果您的建议跟随 决定使用的IAsyncResult 模式:
The following list shows some recommendations to follow if you decide to use the IAsyncResult pattern:
-
只有暴露的IAsyncResult模式,当你特别要求 WaitHandle对于或IAsyncResult的支持 对象。
Only expose the IAsyncResult pattern when you specifically require support for WaitHandle or IAsyncResult objects.
只有暴露的IAsyncResult模式,当你有一个现有的API 它使用的IAsyncResult模式。
Only expose the IAsyncResult pattern when you have an existing API that uses the IAsyncResult pattern.
如果您有一个基于IAsyncResult的图案现有的API,可以考虑 还揭露基于事件的模式 在你的下一个版本。
If you have an existing API based on the IAsyncResult pattern, consider also exposing the event-based pattern in your next release.
只公开IAsyncResult的模式,如果你有高性能 要求您已经验证 不能由基于事件来满足 图案,但可以由满足 IAsyncResult的格局。
Only expose IAsyncResult pattern if you have high performance requirements which you have verified cannot be met by the event-based pattern but can be met by the IAsyncResult pattern.
这篇关于有没有开始*和*异步用于.NET插座之间的性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!