TcpListener.EndAcceptTcpClient(IAsyncResult) TcpListener.EndAcceptTcpClient(IAsyncResult) TcpListener.EndAcceptTcpClient(IAsyncResult) TcpListener.EndAcceptTcpClient(IAsyncResult) Method

Définition

Accepte de manière asynchrone une tentative de connexion entrante et crée un nouveau TcpClient pour gérer les communications de l'hôte distant.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

Paramètres

Retours

TcpClientA TcpClient.

TcpClient utilisé pour envoyer et recevoir des données.The TcpClient used to send and receive data.

Exemples

L’exemple de code suivant illustre l’utilisation de la BeginAcceptTcpClient méthode pour créer et connecter un socket.The following code example demonstrates the use of the BeginAcceptTcpClient method to create and connect a socket. Le délégué de rappel appelle le EndAcceptTcpClient méthode à la fin de la demande asynchrone.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 'DoBeginAcceptTcpClient


' 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 'DoAcceptTcpClientCallback
 

Remarques

Cette méthode se bloque jusqu'à ce que l’opération soit terminer.This method blocks until the operation is complete. Pour effectuer cette opération de façon synchrone, utilisez la AcceptTcpClient (méthode).To perform this operation synchronously, use the AcceptTcpClient method.

Notes

Vous pouvez appeler la RemoteEndPoint propriété du socket sous-jacent (Client) pour identifier l’hôte distant réseau adresse et numéro de port.You can call the RemoteEndPoint property of the underlying socket (Client) to identify the remote host's network address and port number.

Notes

Si vous recevez un SocketException, utilisez le SocketException.ErrorCode propriété pour obtenir le code d’erreur spécifique et reportez-vous à la code d’erreur API Windows Sockets version 2 documentation pour obtenir une description détaillée de l’erreur.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.

Notes

Ce membre génère des informations de traçage lorsque vous activez le traçage réseau dans votre application.This member outputs trace information when you enable network tracing in your application. Pour plus d’informations, consultez le traçage réseau dans le .NET Framework.For more information, see Network Tracing in the .NET Framework.

S’applique à