TcpListener.EndAcceptSocket(IAsyncResult) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Nimmt asynchron einen eingehenden Verbindungsversuch an und erstellt einen neuen Socket zum Behandeln der Remotehostkommunikation.
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
Parameter
- asyncResult
- IAsyncResult
Ein durch einen Aufruf der BeginAcceptSocket(AsyncCallback, Object)-Methode zurückgegebenes IAsyncResult.
Gibt zurück
Ein Socket.
Ein Socket zum Senden und Empfangen von Daten.
Ausnahmen
Der zugrunde liegende Socket wurde geschlossen.
Der asyncResult-Parameter ist null.
Der asyncResult-Parameter wurde nicht durch einen Aufruf der BeginAcceptSocket(AsyncCallback, Object)-Methode erstellt.
Die EndAcceptSocket(IAsyncResult)-Methode wurde bereits zuvor aufgerufen.
Fehler beim Zugriff auf den Socket.
Beispiele
Im folgenden Codebeispiel wird die Verwendung der BeginAcceptSocket Methode zum Erstellen und Verbinden eines Sockets veranschaulicht. Die Rückrufstellvertretung ruft die EndAcceptSocket Methode auf, um die asynchrone Anforderung zu beenden.
// 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
Hinweise
Diese Methode blockiert, bis der Vorgang abgeschlossen ist. Verwenden Sie die AcceptSocket Methode, um diesen Vorgang synchron auszuführen.
Hinweis
Sie können die Eigenschaft des zurückgegebenen Socket Objekts aufrufen, um die RemoteEndPoint Netzwerkadresse und die Portnummer des Remotehosts zu identifizieren.
Hinweis
Wenn Sie eine SocketException, verwenden Sie die SocketException.ErrorCode Eigenschaft, um den spezifischen Fehlercode abzurufen und auf die Windows Sockets Version 2 API-Fehlercodedokumentation zu verweisen, um eine detaillierte Beschreibung des Fehlers zu erhalten.
Hinweis
Dieser Member gibt Ablaufverfolgungsinformationen aus, wenn Sie die Netzwerkablaufverfolgung in der Anwendung aktivieren. Weitere Informationen finden Sie unter "Netzwerkablaufverfolgung" im .NET Framework.