TcpListener.EndAcceptSocket(IAsyncResult) Metoda

Definicja

Asynchronicznie akceptuje próbę połączenia przychodzącego i tworzy nowy Socket do obsługi komunikacji z hostem zdalnym.

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

Parametry

asyncResult
IAsyncResult

Obiekt IAsyncResult zwrócony przez wywołanie BeginAcceptSocket(AsyncCallback, Object) metody .

Zwraca

Socket

Klasa Socket.

Używane do wysyłania i odbierania Socket danych.

Wyjątki

Podstawy Socket zostały zamknięte.

Parametr asyncResult to null.

Parametr asyncResult nie został utworzony przez wywołanie BeginAcceptSocket(AsyncCallback, Object) metody .

Metoda EndAcceptSocket(IAsyncResult) została wcześniej wywołana.

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

Przykłady

Poniższy przykład kodu przedstawia użycie BeginAcceptSocket metody do utworzenia i połączenia gniazda. Delegat wywołania zwrotnego wywołuje metodę EndAcceptSocket , aby zakończyć żądanie asynchroniczne.

    // 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

Uwagi

Ta metoda blokuje działanie do momentu ukończenia operacji. Aby wykonać tę operację synchronicznie, użyj AcceptSocket metody .

Uwaga

Możesz wywołać RemoteEndPoint właściwość zwróconej Socket w celu zidentyfikowania adresu sieciowego i numeru portu hosta zdalnego.

Uwaga

Jeśli wystąpi błąd , użyj SocketException.ErrorCode właściwości , aby uzyskać określony kod błędu i zapoznaj się z dokumentacją kodu błędu interfejsu API w wersji 2 Windows Sockets w wersji 2, aby uzyskać szczegółowy opis błędu.SocketException

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