Socket.DisconnectAsync Method

Definition

Overloads

DisconnectAsync(SocketAsyncEventArgs)

Begins an asynchronous request to disconnect from a remote endpoint.

DisconnectAsync(Boolean, CancellationToken)

Disconnects a connected socket from the remote host.

DisconnectAsync(SocketAsyncEventArgs)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Begins an asynchronous request to disconnect from a remote endpoint.

public bool DisconnectAsync (System.Net.Sockets.SocketAsyncEventArgs e);

Parameters

e
SocketAsyncEventArgs

The SocketAsyncEventArgs object to use for this asynchronous socket operation.

Returns

true if the I/O operation is pending. The Completed event on the e parameter will be raised upon completion of the operation.

false if the I/O operation completed synchronously. In this case, The Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.

Exceptions

The e parameter cannot be null.

A socket operation was already in progress using the SocketAsyncEventArgs object specified in the e parameter.

The Socket has been closed.

An error occurred when attempting to access the socket.

Remarks

When using a connection-oriented protocol, calling the DisconnectAsync method requests a disconnect from a remote endpoint. If you set SocketAsyncEventArgs.DisconnectReuseSocket to true in the e parameter, the socket can be reused.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DisconnectAsync(Boolean, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Disconnects a connected socket from the remote host.

public System.Threading.Tasks.ValueTask DisconnectAsync (bool reuseSocket, System.Threading.CancellationToken cancellationToken = default);

Parameters

reuseSocket
Boolean

Indicates whether the socket should be available for reuse after disconnect.

cancellationToken
CancellationToken

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

Returns

An asynchronous task that completes when the socket is disconnected.

Exceptions

The Socket object has been closed.

An error occurred when attempting to access the socket.

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

Remarks

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 Disconnect(Boolean).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9