TcpListener.EndAcceptTcpClient(IAsyncResult) Methode

Definition

Nimmt asynchron einen eingehenden Verbindungsversuch an und erstellt einen neuen TcpClient zum Behandeln der Remotehostkommunikation.Asynchronously accepts an incoming connection attempt and creates a new TcpClient to handle remote host communication.

public:
 System::Net::Sockets::TcpClient ^ EndAcceptTcpClient(IAsyncResult ^ asyncResult);
public System.Net.Sockets.TcpClient EndAcceptTcpClient (IAsyncResult asyncResult);
member this.EndAcceptTcpClient : IAsyncResult -> System.Net.Sockets.TcpClient
Public Function EndAcceptTcpClient (asyncResult As IAsyncResult) As TcpClient

Parameter

asyncResult
IAsyncResult

Ein durch einen Aufruf der BeginAcceptTcpClient(AsyncCallback, Object)-Methode zurückgegebenes IAsyncResult.An IAsyncResult returned by a call to the BeginAcceptTcpClient(AsyncCallback, Object) method.

Gibt zurück

TcpClient

Ein TcpClient.A TcpClient.

Ein TcpClient zum Senden und Empfangen von Daten.The TcpClient used to send and receive data.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die BeginAcceptTcpClient -Methode verwendet wird, um einen Socket zu erstellen und zu verbinden.The following code example demonstrates the use of the BeginAcceptTcpClient method to create and connect a socket. Der Rückruf Delegat ruft die- EndAcceptTcpClient Methode auf, um die asynchrone Anforderung zu beenden.The callback delegate calls the EndAcceptTcpClient method to end the asynchronous request.

    // Thread signal.
public:
    static ManualResetEvent^ TcpClientConnected;

    // Accept one client connection asynchronously.
public:
    static void DoBeginAcceptTcpClient(TcpListener^ listener)
    {
        // Set the event to nonsignaled state.
        TcpClientConnected->Reset();

        // Start to listen for connections from a client.
        Console::WriteLine("Waiting for a connection...");

        // Accept the connection.
        // BeginAcceptSocket() creates the accepted socket.
        listener->BeginAcceptTcpClient(
            gcnew AsyncCallback(DoAcceptTcpClientCallback),
            listener);

        // Wait until a connection is made and processed before
        // continuing.
        TcpClientConnected->WaitOne();
    }

    // Process the client connection.
public:
    static void DoAcceptTcpClientCallback(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.
        TcpClient^ client = listener->EndAcceptTcpClient(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.
        TcpClientConnected->Set();

    }
// Thread signal.
public static ManualResetEvent tcpClientConnected =
    new ManualResetEvent(false);

// Accept one client connection asynchronously.
public static void DoBeginAcceptTcpClient(TcpListener
    listener)
{
    // Set the event to nonsignaled state.
    tcpClientConnected.Reset();

    // Start to listen for connections from a client.
    Console.WriteLine("Waiting for a connection...");

    // Accept the connection.
    // BeginAcceptSocket() creates the accepted socket.
    listener.BeginAcceptTcpClient(
        new AsyncCallback(DoAcceptTcpClientCallback),
        listener);

    // Wait until a connection is made and processed before
    // continuing.
    tcpClientConnected.WaitOne();
}

// Process the client connection.
public static void DoAcceptTcpClientCallback(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.
    TcpClient client = listener.EndAcceptTcpClient(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.
    tcpClientConnected.Set();
}
' Thread signal.
Public Shared tcpClientConnected As New ManualResetEvent(False)


' Accept one client connection asynchronously.
Public Shared Sub DoBeginAcceptTcpClient(listener As TcpListener)
   ' Set the event to nonsignaled state.
   tcpClientConnected.Reset()
   
   ' Start to listen for connections from a client.
   Console.WriteLine("Waiting for a connection...")
   
   ' Accept the connection. 
   ' BeginAcceptSocket() creates the accepted socket.
   listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf DoAcceptTcpClientCallback), listener)
   
   ' Wait until a connection is made and processed before 
   ' continuing.
   tcpClientConnected.WaitOne()
End Sub


' Process the client connection.
Public Shared Sub DoAcceptTcpClientCallback(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 client As TcpClient = listener.EndAcceptTcpClient(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.
   tcpClientConnected.Set()
End Sub
 

Hinweise

Diese Methode wird blockiert, bis der Vorgang beendet ist.This method blocks until the operation is complete. Verwenden Sie die-Methode, um diesen Vorgang synchron auszuführen AcceptTcpClient .To perform this operation synchronously, use the AcceptTcpClient method.

Hinweis

Sie können die RemoteEndPoint -Eigenschaft des zugrunde liegenden Sockets ( Client ) zum Identifizieren der Netzwerkadresse und der Portnummer des Remote Hosts aufzurufen.You can call the RemoteEndPoint property of the underlying socket (Client) to identify the remote host's network address and port number.

Hinweis

Wenn Sie einen erhalten SocketException , verwenden Sie die SocketException.ErrorCode -Eigenschaft, um den spezifischen Fehlercode abzurufen. eine ausführliche Beschreibung des Fehlers finden Sie in der API-Fehlercode Dokumentation von Windows Sockets, Version 2 .If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Hinweis

Dieser Member gibt Ablaufverfolgungsinformationen aus, wenn Sie die Netzwerkablaufverfolgung in der Anwendung aktivieren.This member outputs trace information when you enable network tracing in your application. Weitere Informationen finden Sie unter Netzwerk Ablauf Verfolgung in der .NET Framework.For more information, see Network Tracing in the .NET Framework.

Gilt für: