Edit

Share via


TcpListener.AcceptTcpClientAsync Method

Definition

Overloads

AcceptTcpClientAsync()

Accepts a pending connection request as an asynchronous operation.

AcceptTcpClientAsync(CancellationToken)

Accepts a pending connection request as a cancellable asynchronous operation.

AcceptTcpClientAsync()

Source:
TCPListener.cs
Source:
TCPListener.cs
Source:
TCPListener.cs

Accepts a pending connection request as an asynchronous operation.

public:
 System::Threading::Tasks::Task<System::Net::Sockets::TcpClient ^> ^ AcceptTcpClientAsync();
public System.Threading.Tasks.Task<System.Net.Sockets.TcpClient> AcceptTcpClientAsync ();
member this.AcceptTcpClientAsync : unit -> System.Threading.Tasks.Task<System.Net.Sockets.TcpClient>
Public Function AcceptTcpClientAsync () As Task(Of TcpClient)

Returns

The task object representing the asynchronous operation. The Result property on the task object returns a TcpClient used to send and receive data.

Exceptions

The listener has not been started with a call to Start().

Use the ErrorCode property to obtain the specific error code. When you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Remarks

This operation will not block. The returned Task<TResult> object will complete after the TCP connection has been accepted.

Use the TcpClient.GetStream method to obtain the underlying NetworkStream of the returned TcpClient in the Task<TResult>. The NetworkStream will provide you with methods for sending and receiving with the remote host. When you are through with the TcpClient, be sure to call its Close method. If you want greater flexibility than a TcpClient offers, consider using AcceptSocket or AcceptSocketAsync.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by AcceptTcpClient().

See also

Applies to

AcceptTcpClientAsync(CancellationToken)

Source:
TCPListener.cs
Source:
TCPListener.cs
Source:
TCPListener.cs

Accepts a pending connection request as a cancellable asynchronous operation.

public:
 System::Threading::Tasks::ValueTask<System::Net::Sockets::TcpClient ^> AcceptTcpClientAsync(System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.ValueTask<System.Net.Sockets.TcpClient> AcceptTcpClientAsync (System.Threading.CancellationToken cancellationToken);
member this.AcceptTcpClientAsync : System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<System.Net.Sockets.TcpClient>
Public Function AcceptTcpClientAsync (cancellationToken As CancellationToken) As ValueTask(Of TcpClient)

Parameters

cancellationToken
CancellationToken

A cancellation token that can be used to cancel the asynchronous operation

Returns

The task object representing the asynchronous operation. The Result property on the task object returns a TcpClient used to send and receive data.

Exceptions

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This operation will not block. The returned Task<TResult> object will complete after the TCP connection has been accepted.

Use the TcpClient.GetStream method to obtain the underlying NetworkStream of the returned TcpClient in the Task<TResult>. The NetworkStream will provide you with methods for sending and receiving with the remote host. When you are through with the TcpClient, be sure to call its Close method. If you want greater flexibility than a TcpClient offers, consider using AcceptSocket or AcceptSocketAsync.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by AcceptTcpClient().

Applies to