Socket.EndDisconnect(IAsyncResult) Metoda

Definicja

Kończy oczekujące żądanie asynchronicznego rozłączenia.

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

Parametry

asyncResult
IAsyncResult

IAsyncResult Obiekt, który przechowuje informacje o stanie i wszystkie dane zdefiniowane przez użytkownika dla tej operacji asynchronicznej.

Wyjątki

Obiekt Socket został zamknięty.

asyncResult to null.

asyncResult element nie został zwrócony przez wywołanie BeginDisconnect(Boolean, AsyncCallback, Object) metody .

EndDisconnect(IAsyncResult) wcześniej wywoływano połączenie asynchroniczne.

Wystąpił błąd podczas próby uzyskania dostępu do gniazda.

Upłynął limit czasu żądania rozłączenia.

Przykłady

Poniższy przykład kodu tworzy gniazdo do komunikacji asynchronicznej i wysyła pewne dane do hosta zdalnego. Po wysłaniu danych jest wywoływana, Shutdown aby zatrzymać działanie wysyłania i odbierania. Następnie BeginDisconnect jest wywoływana, aby rozpocząć żądanie rozłączenia. Delegat wywołania zwrotnego wywołuje, EndDisconnect aby zakończyć żądanie asynchroniczne. Po zakończeniu żądania właściwość jest odpytywane w celu sprawdzenia, Connected czy gniazdo jest rozłączone.

      // 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();
}

Uwagi

EndDisconnect wykonuje wywołanie metody BeginDisconnect. Metoda EndDisconnect blokuje się do momentu zakończenia rozłączenia. Aby uzyskać informacje o operacjach asynchronicznych, zobacz Asynchronous Programming Model (APM).

Uwaga

Jeśli zostanie wyświetlony element SocketException, użyj SocketException.ErrorCode właściwości , aby uzyskać określony kod błędu. Po uzyskaniu tego kodu zapoznaj się z dokumentacją kodu błędu interfejsu API Windows Sockets w wersji 2, aby uzyskać szczegółowy opis błędu.

Uwaga

Ten element członkowski generuje informacje ze śledzenia pod warunkiem włączenia funkcji śledzenia sieci w aplikacji. Aby uzyskać więcej informacji, zobacz Śledzenie sieci w .NET Framework.

Dotyczy