问题描述
BeginConnect
和 ConnectAsync
有什么区别?随后,BeginDisconnect
和 DisconnectAsync
有什么区别?
What is the difference between BeginConnect
and ConnectAsync
? Subsequently, what is the difference between BeginDisconnect
and DisconnectAsync
?
ConnectAsync
文档说明:
开始对远程主机连接的异步请求."
BeginConnect
文档还指出:
开始对远程主机连接的异步请求."
DisconnectAsync
和 BeginDisconnect
也说明同样的事情:
Both the DisconnectAsync
and BeginDisconnect
also state the same thing:
开始异步请求与远程端点断开连接."
这些方法对之间有什么区别,应该使用哪一个?
What's the difference between those method pairs and which one should be used?
推荐答案
Socket.ConnectAsync 提供 SocketAsyncEventArgs 作为参数,与 BeginConnect 提供的 3 个参数相比,它包含更多信息.另外我知道 ConnectAsync 的引入晚于 BeginConnect 并解决了一些与超时相关的问题(现在不记得这个讨论的来源了).尽可能使用 ConnectAsync(尽管它需要最低 .NET 2.0 SP1).
Socket.ConnectAsync provides SocketAsyncEventArgs as a parameters which contains a lot more information compared to 3 params provided by BeginConnect. Also I know that ConnectAsync introduced later than BeginConnect and solves some issues related to timeouts (cannot remember the source of this discussion now). Prefer ConnectAsync when possible (though it requires min .NET 2.0 SP1).
ConnectAsync 有一个关于回调的问题.如果这是一个问题,这里是关于它的讨论:使用 System.Net.Sockets.Socket.AcceptAsync 模型时的堆栈溢出 和 AsyncCallBack CompletedSynchronously
There is a catch with ConnectAsync about the callbacks. If this is of concern, here are the discussions about it: Stack overflow when using the System.Net.Sockets.Socket.AcceptAsync model and AsyncCallBack CompletedSynchronously
Silverlight 中不支持 BeginConnect 方法(仅支持 ConnectAsync),因此如果您打算开发客户端 Silverlight 应用程序,这可能是另一个问题.
There is no support for BeginConnect method in Silverlight (only ConnectAsync is supported) so that may be another concern if you intend to develop client side Silverlight applications.
此外,两种方法中使用的模式也不同.这是讨论:是.NET 中的套接字的 Begin* 和 *Async 之间有什么性能差异?
Also the patterns used in two approaches are different. Here is the discussion: Is there any performance difference between Begin* and *Async for sockets in .NET?
这篇关于BeginConnect 和 ConnectAsync 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!