Socket.ReceiveAsync Method

Definition

Overloads

ReceiveAsync(ArraySegment<Byte>)

Receives data from a connected socket.

ReceiveAsync(IList<ArraySegment<Byte>>)

Receives data from a connected socket.

ReceiveAsync(SocketAsyncEventArgs)

Begins an asynchronous request to receive data from a connected Socket object.

ReceiveAsync(ArraySegment<Byte>, SocketFlags)

Receives data from a connected socket.

ReceiveAsync(IList<ArraySegment<Byte>>, SocketFlags)

Receives data from a connected socket.

ReceiveAsync(Memory<Byte>, CancellationToken)

Receives data from a connected socket.

ReceiveAsync(Memory<Byte>, SocketFlags, CancellationToken)

Receives data from a connected socket.

ReceiveAsync(ArraySegment<Byte>)

Receives data from a connected socket.

public:
 System::Threading::Tasks::Task<int> ^ ReceiveAsync(ArraySegment<System::Byte> buffer);
public System.Threading.Tasks.Task<int> ReceiveAsync (ArraySegment<byte> buffer);
member this.ReceiveAsync : ArraySegment<byte> -> System.Threading.Tasks.Task<int>
Public Function ReceiveAsync (buffer As ArraySegment(Of Byte)) As Task(Of Integer)

Parameters

buffer
ArraySegment<Byte>

The buffer for the received data.

Returns

An asynchronous task that completes with the number of bytes received.

Exceptions

The Socket has been closed.

An error occurred when attempting to access the socket.

Applies to

ReceiveAsync(IList<ArraySegment<Byte>>)

Receives data from a connected socket.

public:
 System::Threading::Tasks::Task<int> ^ ReceiveAsync(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers);
public System.Threading.Tasks.Task<int> ReceiveAsync (System.Collections.Generic.IList<ArraySegment<byte>> buffers);
member this.ReceiveAsync : System.Collections.Generic.IList<ArraySegment<byte>> -> System.Threading.Tasks.Task<int>
Public Function ReceiveAsync (buffers As IList(Of ArraySegment(Of Byte))) As Task(Of Integer)

Parameters

buffers
IList<ArraySegment<Byte>>

A list of buffers for the received data.

Returns

An asynchronous task that completes with the number of bytes received.

Exceptions

The buffers parameter was null.

The Socket has been closed.

An error occurred when attempting to access the socket.

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 Receive(IList<ArraySegment<Byte>>).

Applies to

ReceiveAsync(SocketAsyncEventArgs)

Begins an asynchronous request to receive data from a connected Socket object.

public:
 bool ReceiveAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool ReceiveAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.ReceiveAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function ReceiveAsync (e As SocketAsyncEventArgs) As Boolean

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

An argument was invalid. The Buffer or BufferList properties on the e parameter must reference valid buffers. One or the other of these properties may be set, but not both at the same time.

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

The ReceiveAsync method is used on connected sockets or bound connectionless sockets and is used to read incoming data. The socket's local address must be known.

For bound connectionless sockets, this function restricts the addresses from which received messages are accepted. The function only returns messages from the remote address specified in the connection. Messages from other addresses are silently discarded.

The SocketAsyncEventArgs.SocketFlags property on the e parameter provides the Window Sockets service provider with additional information about the read request. For more information about how to use this parameter, see System.Net.Sockets.SocketFlags.

The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required to successfully call this method:

The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the ReceiveAsync method, so that the information will be retrievable in the callback method. If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.

For byte stream-style sockets, incoming data is placed into the buffer until the buffer is filled, the connection is closed, or the internally buffered data is exhausted.

For message-oriented sockets, an incoming message is placed into the buffer up to the total size of the buffer associated with the e parameter. If the message is larger than the buffer, the buffer is filled with the first part of the message.

For connection-oriented sockets, the ReceiveAsync method can indicate the graceful termination of the virtual circuit in one of two ways that depend on whether the socket is byte stream or message oriented. For byte streams, zero bytes having been read indicates graceful closure and that no more bytes will ever be read. For message-oriented sockets, where a zero byte message is often allowable, a SocketException with the SocketAsyncEventArgs.SocketError set to the native Winsock WSAEDISCON error code (10101) is used to indicate graceful closure. In any case, a SocketException with the SocketAsyncEventArgs.SocketError set to the native Winsock WSAECONNRESET error code (10054) indicates an abortive close has occurred.

See also

Applies to

ReceiveAsync(ArraySegment<Byte>, SocketFlags)

Receives data from a connected socket.

public:
 System::Threading::Tasks::Task<int> ^ ReceiveAsync(ArraySegment<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags);
public System.Threading.Tasks.Task<int> ReceiveAsync (ArraySegment<byte> buffer, System.Net.Sockets.SocketFlags socketFlags);
member this.ReceiveAsync : ArraySegment<byte> * System.Net.Sockets.SocketFlags -> System.Threading.Tasks.Task<int>
Public Function ReceiveAsync (buffer As ArraySegment(Of Byte), socketFlags As SocketFlags) As Task(Of Integer)

Parameters

buffer
ArraySegment<Byte>

The buffer for the received data.

socketFlags
SocketFlags

A bitwise combination of SocketFlags values that will be used when receiving the data.

Returns

An asynchronous task that completes with the number of bytes received.

Exceptions

The Socket has been closed.

An error occurred when attempting to access the socket.

Applies to

ReceiveAsync(IList<ArraySegment<Byte>>, SocketFlags)

Receives data from a connected socket.

public:
 System::Threading::Tasks::Task<int> ^ ReceiveAsync(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags);
public System.Threading.Tasks.Task<int> ReceiveAsync (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags);
member this.ReceiveAsync : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags -> System.Threading.Tasks.Task<int>
Public Function ReceiveAsync (buffers As IList(Of ArraySegment(Of Byte)), socketFlags As SocketFlags) As Task(Of Integer)

Parameters

buffers
IList<ArraySegment<Byte>>

A list of buffers for the received data.

socketFlags
SocketFlags

A bitwise combination of SocketFlags values that will be used when receiving the data.

Returns

An asynchronous task that completes with the number of bytes received.

Exceptions

The buffers parameter was null.

The Socket has been closed.

An error occurred when attempting to access the socket.

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 Receive(IList<ArraySegment<Byte>>, SocketFlags).

Applies to

ReceiveAsync(Memory<Byte>, CancellationToken)

Receives data from a connected socket.

public System.Threading.Tasks.ValueTask<int> ReceiveAsync (Memory<byte> buffer, System.Threading.CancellationToken cancellationToken = default);
member this.ReceiveAsync : Memory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function ReceiveAsync (buffer As Memory(Of Byte), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

Parameters

buffer
Memory<Byte>

The buffer for the received data.

cancellationToken
CancellationToken

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

Returns

An asynchronous task that completes with the number of bytes received.

Exceptions

The Socket 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.

Applies to

ReceiveAsync(Memory<Byte>, SocketFlags, CancellationToken)

Receives data from a connected socket.

public System.Threading.Tasks.ValueTask<int> ReceiveAsync (Memory<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Threading.CancellationToken cancellationToken = default);
member this.ReceiveAsync : Memory<byte> * System.Net.Sockets.SocketFlags * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function ReceiveAsync (buffer As Memory(Of Byte), socketFlags As SocketFlags, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

Parameters

buffer
Memory<Byte>

The buffer for the received data.

socketFlags
SocketFlags

A bitwise combination of SocketFlags values that will be used when receiving the data.

cancellationToken
CancellationToken

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

Returns

An asynchronous task that completes with the number of bytes received.

Exceptions

The Socket 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.

Applies to