Socket.BeginDisconnect(Boolean, AsyncCallback, Object) Socket.BeginDisconnect(Boolean, AsyncCallback, Object) Socket.BeginDisconnect(Boolean, AsyncCallback, Object) Socket.BeginDisconnect(Boolean, AsyncCallback, Object) Method

定义

开始异步请求从远程终结点断开连接。Begins an asynchronous request to disconnect from a remote endpoint.

public:
 IAsyncResult ^ BeginDisconnect(bool reuseSocket, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state);
member this.BeginDisconnect : bool * AsyncCallback * obj -> IAsyncResult
Public Function BeginDisconnect (reuseSocket As Boolean, callback As AsyncCallback, state As Object) As IAsyncResult

参数

reuseSocket
Boolean Boolean Boolean Boolean

如果关闭该连接后可以重用此套接字,则为 true;否则为 falsetrue if this socket can be reused after the connection is closed; otherwise, false.

state
Object Object Object Object

一个对象,它包含此请求的状态信息。An object that contains state information for this request.

返回

一个 IAsyncResult 对象,它引用异步操作。An IAsyncResult object that references the 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 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. 完成请求后,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();
}

注解

如果使用的是面向连接的协议,则可以调用BeginDisconnect方法来请求从远程终结点断开连接。If you are using a connection-oriented protocol, you can call the BeginDisconnect method to request a disconnect from a remote endpoint. 如果reuseSockettrue,可以重复使用套接字。If reuseSocket is true, you can reuse the socket.

BeginDisconnect方法使用一个单独的线程来调用指定的回调方法。The BeginDisconnect method uses a separate thread to invoke the specified callback method. EndDisconnect方法进行阻止,直到挂起的断开连接已完成。The EndDisconnect method blocks until the pending disconnect is complete. 有关编写回调方法的其他信息,请参阅委托作为回调方法进行封送For additional information on writing callback methods, see Marshaling a Delegate as a Callback Method.

备注

如果你收到SocketException异常,请使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException exception, 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.

适用于