Socket.EndDisconnect(IAsyncResult) Socket.EndDisconnect(IAsyncResult) Socket.EndDisconnect(IAsyncResult) Socket.EndDisconnect(IAsyncResult) Method

定义

结束挂起的异步断开连接请求。Ends a pending asynchronous disconnect request.

public:
 void EndDisconnect(IAsyncResult ^ asyncResult);
public void EndDisconnect (IAsyncResult asyncResult);
member this.EndDisconnect : IAsyncResult -> unit
Public Sub EndDisconnect (asyncResult As IAsyncResult)

参数

asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

IAsyncResult 对象,它存储此异步操作的状态信息以及所有用户定义数据。An IAsyncResult object that stores state information and any user-defined data for this asynchronous operation.

异常

操作系统为 Windows 2000 或更低版本,而此方法需要在 Windows XP 中使用。The operating system is Windows 2000 or earlier, and this method requires Windows XP.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

断开连接请求已超时。The disconnect request has timed out.

示例

下面的代码示例创建的套接字进行异步通信,并将一些数据发送到远程主机。The following code example creates a socket for asynchronous communication and sends some data to a remote host. 已发送数据,Shutdown调用来停止发送和接收活动。When the data has been sent, Shutdown is called to stop the send and receive activity. 然后BeginDisconnect调用以开始断开连接请求。Then BeginDisconnect is called to begin a disconnect request. 回调委托调用EndDisconnect结束异步请求。The callback delegate calls EndDisconnect to end the asynchronous request. 完成请求后,Connected属性进行查询,以测试是否已断开连接的套接字。When the request completes, the Connected property is queried to test whether the socket is disconnected.

      // Establish the remote endpoint for the socket.
      // For this example use local computer.
      IPHostEntry^ ipHostInfo = Dns::GetHostEntry( Dns::GetHostName() );
      IPAddress^ ipAddress = ipHostInfo->AddressList[ 0 ];
      IPEndPoint^ remoteEP = gcnew IPEndPoint( ipAddress,11000 );

      // Create a TCP/IP socket.
      Socket^ client = gcnew Socket( AddressFamily::InterNetwork,SocketType::Stream,ProtocolType::Tcp );

      // Connect to the remote endpoint.
      client->BeginConnect( remoteEP, gcnew AsyncCallback( ConnectCallback ), client );

      // Wait for connect.
      connectDone->WaitOne();

      // Send some data to the remote device.
      String^ data = "This is a string of data <EOF>";
      array<Byte>^buffer = Encoding::ASCII->GetBytes( data );
      client->BeginSend( buffer, 0, buffer->Length, static_cast<SocketFlags>(0), gcnew AsyncCallback( ClientSendCallback ), client );

      // Wait for send done.
      sendDone->WaitOne();

      // Release the socket.
      client->Shutdown( SocketShutdown::Both );
      client->BeginDisconnect( true, gcnew AsyncCallback( DisconnectCallback ), client );

      // Wait for the disconnect to complete.
      disconnectDone->WaitOne();
      if ( client->Connected )
            Console::WriteLine( "We're still connected" );
      else
            Console::WriteLine( "We're disconnected" );
   }

private:
   static void DisconnectCallback( IAsyncResult^ ar )
   {
      // Complete the disconnect request.
      Socket^ client = dynamic_cast<Socket^>(ar->AsyncState);
      client->EndDisconnect( ar );

      // Signal that the disconnect is complete.
      disconnectDone->Set();
   }

public:

    // Establish the remote endpoint for the socket.
    // For this example use local computer.
    IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

    // Create a TCP/IP socket.
    Socket client = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp);

    // Connect to the remote endpoint.
    client.BeginConnect(remoteEP, 
        new AsyncCallback(ConnectCallback), client);
        
    // Wait for connect.
    connectDone.WaitOne();

    // Send some data to the remote device.
    string data = "This is a string of data <EOF>";
    byte[] buffer = Encoding.ASCII.GetBytes(data);
    client.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(ClientSendCallback), client);
    // Wait for send done.
    sendDone.WaitOne();

    // Release the socket.
    client.Shutdown(SocketShutdown.Both);
    client.BeginDisconnect(true, new AsyncCallback(DisconnectCallback), client);

    // Wait for the disconnect to complete.
    disconnectDone.WaitOne();
    if (client.Connected)
        Console.WriteLine("We're still connected");
    else
        Console.WriteLine("We're disconnected");
}


private static void DisconnectCallback(IAsyncResult ar)
{ 
    // Complete the disconnect request.
    Socket client = (Socket) ar.AsyncState;
    client.EndDisconnect(ar);

    // Signal that the disconnect is complete.
    disconnectDone.Set();
}

注解

EndDisconnect 完成对调用BeginDisconnectEndDisconnect completes a call to BeginDisconnect. EndDisconnect方法进行阻止,直到完成断开连接。The EndDisconnect method blocks until the disconnect completes. 有关异步操作的信息,请参阅 MSDN library 中的异步编程的概述主题。For information about asynchronous operations, see the Asynchronous Programming Overview topic in the MSDN library.

备注

如果你收到SocketException,使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码有关错误的详细说明的文档。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅在.NET Framework 中进行网络跟踪For more information, see Network Tracing in the .NET Framework.

适用于