TcpListener.EndAcceptSocket(IAsyncResult) 方法 (System.Net.Sockets)

您所在的位置:网站首页 socketlisten参数 TcpListener.EndAcceptSocket(IAsyncResult) 方法 (System.Net.Sockets)

TcpListener.EndAcceptSocket(IAsyncResult) 方法 (System.Net.Sockets)

2023-03-04 20:32| 来源: 网络整理| 查看: 265

TcpListener.EndAcceptSocket(IAsyncResult) 方法 参考 定义 命名空间: System.Net.Sockets 程序集:System.Net.Sockets.dll 程序集:System.dll 程序集:netstandard.dll

重要

一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。

异步接受传入的连接尝试,并创建新的 Socket 来处理远程主机通信。

public: System::Net::Sockets::Socket ^ EndAcceptSocket(IAsyncResult ^ asyncResult); public System.Net.Sockets.Socket EndAcceptSocket (IAsyncResult asyncResult); member this.EndAcceptSocket : IAsyncResult -> System.Net.Sockets.Socket Public Function EndAcceptSocket (asyncResult As IAsyncResult) As Socket 参数 asyncResult IAsyncResult

通过调用 IAsyncResult 方法返回 BeginAcceptSocket(AsyncCallback, Object)。

返回 Socket

Socket。

用于发送和接收数据的 Socket。

例外 ObjectDisposedException

已关闭基础 Socket。

ArgumentNullException

asyncResult 参数为 null。

ArgumentException

调用 BeginAcceptSocket(AsyncCallback, Object) 方法后,未创建 asyncResult 参数。

InvalidOperationException

EndAcceptSocket(IAsyncResult) 方法以前被调用过。

SocketException

尝试访问 Socket 时发生错误。

示例

下面的代码示例演示如何使用 BeginAcceptSocket 方法创建和连接套接字。 回调委托调用 EndAcceptSocket 方法以结束异步请求。

// Thread signal. public: static ManualResetEvent^ ClientConnected; // Accept one client connection asynchronously. public: static void DoBeginAcceptSocket(TcpListener^ listener) { // Set the event to nonsignaled state. ClientConnected->Reset(); // Start to listen for connections from a client. Console::WriteLine("Waiting for a connection..."); // Accept the connection. // BeginAcceptSocket() creates the accepted socket. listener->BeginAcceptSocket( gcnew AsyncCallback(DoAcceptSocketCallback), listener); // Wait until a connection is made and processed before // continuing. ClientConnected->WaitOne(); } // Process the client connection. public: static void DoAcceptSocketCallback(IAsyncResult^ result) { // Get the listener that handles the client request. TcpListener^ listener = (TcpListener^) result->AsyncState; // End the operation and display the received data on the //console. Socket^ clientSocket = listener->EndAcceptSocket(result); // Process the connection here. (Add the client to a // server table, read data, etc.) Console::WriteLine("Client connected completed"); // Signal the calling thread to continue. ClientConnected->Set(); } // Thread signal. public static ManualResetEvent clientConnected = new ManualResetEvent(false); // Accept one client connection asynchronously. public static void DoBeginAcceptSocket(TcpListener listener) { // Set the event to nonsignaled state. clientConnected.Reset(); // Start to listen for connections from a client. Console.WriteLine("Waiting for a connection..."); // Accept the connection. // BeginAcceptSocket() creates the accepted socket. listener.BeginAcceptSocket( new AsyncCallback(DoAcceptSocketCallback), listener); // Wait until a connection is made and processed before // continuing. clientConnected.WaitOne(); } // Process the client connection. public static void DoAcceptSocketCallback(IAsyncResult ar) { // Get the listener that handles the client request. TcpListener listener = (TcpListener) ar.AsyncState; // End the operation and display the received data on the //console. Socket clientSocket = listener.EndAcceptSocket(ar); // Process the connection here. (Add the client to a // server table, read data, etc.) Console.WriteLine("Client connected completed"); // Signal the calling thread to continue. clientConnected.Set(); } ' Thread signal. Public Shared clientConnected As New ManualResetEvent(False) ' Accept one client connection asynchronously. Public Shared Sub DoBeginAcceptSocket(listener As TcpListener) ' Set the event to nonsignaled state. clientConnected.Reset() ' Start to listen for connections from a client. Console.WriteLine("Waiting for a connection...") ' Accept the connection. ' BeginAcceptSocket() creates the accepted socket. listener.BeginAcceptSocket(New AsyncCallback(AddressOf DoAcceptSocketCallback), listener) ' Wait until a connection is made and processed before ' continuing. clientConnected.WaitOne() End Sub ' Process the client connection. Public Shared Sub DoAcceptSocketCallback(ar As IAsyncResult) ' Get the listener that handles the client request. Dim listener As TcpListener = CType(ar.AsyncState, TcpListener) ' End the operation and display the received data on the 'console. Dim clientSocket As Socket = listener.EndAcceptSocket(ar) ' Process the connection here. (Add the client to a ' server table, read data, etc.) Console.WriteLine("Client connected completed") ' Signal the calling thread to continue. clientConnected.Set() End Sub 注解

此方法将一直阻止,直到操作完成。 若要同步执行此操作,请使用 AcceptSocket 方法。

注意

可以调用 RemoteEndPoint 返回 Socket 的 的 属性,以标识远程主机的网络地址和端口号。

注意

如果收到 SocketException,请使用 SocketException.ErrorCode 属性获取特定的错误代码,并参阅 Windows 套接字版本 2 API 错误代码 文档以获取错误的详细说明。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪。

适用于


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3