Edit

Share via


Socket.ReceiveFromAsync Method

Definition

Overloads

ReceiveFromAsync(SocketAsyncEventArgs)

Begins to asynchronously receive data from a specified network device.

ReceiveFromAsync(ArraySegment<Byte>, EndPoint)

Receives data and returns the endpoint of the sending host.

ReceiveFromAsync(ArraySegment<Byte>, SocketFlags, EndPoint)

Receives data and returns the endpoint of the sending host.

ReceiveFromAsync(Memory<Byte>, EndPoint, CancellationToken)

Receives data and returns the endpoint of the sending host.

ReceiveFromAsync(Memory<Byte>, SocketFlags, EndPoint, CancellationToken)

Receives data and returns the endpoint of the sending host.

ReceiveFromAsync(Memory<Byte>, SocketFlags, SocketAddress, CancellationToken)

Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

ReceiveFromAsync(SocketAsyncEventArgs)

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

Begins to asynchronously receive data from a specified network device.

public:
 bool ReceiveFromAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool ReceiveFromAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.ReceiveFromAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function ReceiveFromAsync (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

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 ReceiveFromAsync method is used primarily to receive data on a connectionless socket. The socket's local address must be known.

The caller must set the SocketAsyncEventArgs.RemoteEndPoint property to an EndPoint of the same type as the endpoint of the remote host. The property will be updated on successful receive to the actual remote endpoint.

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 ReceiveFromAsync 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 message-oriented sockets, an incoming message is placed into the buffer up to the total size of the buffer. The SocketAsyncEventArgs.Count and SocketAsyncEventArgs.Offset properties determine where in the buffer the data is placed and the amount of data.

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. The SocketAsyncEventArgs.Count and SocketAsyncEventArgs.Offset properties determine where in the buffer the data is placed and the amount of data.

See also

Applies to

ReceiveFromAsync(ArraySegment<Byte>, EndPoint)

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

Receives data and returns the endpoint of the sending host.

public:
 System::Threading::Tasks::Task<System::Net::Sockets::SocketReceiveFromResult> ^ ReceiveFromAsync(ArraySegment<System::Byte> buffer, System::Net::EndPoint ^ remoteEndPoint);
public System.Threading.Tasks.Task<System.Net.Sockets.SocketReceiveFromResult> ReceiveFromAsync (ArraySegment<byte> buffer, System.Net.EndPoint remoteEndPoint);
member this.ReceiveFromAsync : ArraySegment<byte> * System.Net.EndPoint -> System.Threading.Tasks.Task<System.Net.Sockets.SocketReceiveFromResult>
Public Function ReceiveFromAsync (buffer As ArraySegment(Of Byte), remoteEndPoint As EndPoint) As Task(Of SocketReceiveFromResult)

Parameters

buffer
ArraySegment<Byte>

The buffer for the received data.

remoteEndPoint
EndPoint

An endpoint of the same type as the endpoint of the remote host.

Returns

An asynchronous task that completes with a SocketReceiveFromResult containing the number of bytes received and the endpoint of the sending host.

Exceptions

remoteEndPoint is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

A caller in the call stack does not have the required permissions.

Applies to

ReceiveFromAsync(ArraySegment<Byte>, SocketFlags, EndPoint)

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

Receives data and returns the endpoint of the sending host.

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

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.

remoteEndPoint
EndPoint

An endpoint of the same type as the endpoint of the remote host.

Returns

An asynchronous task that completes with a SocketReceiveFromResult containing the number of bytes received and the endpoint of the sending host.

Exceptions

remoteEndPoint is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

A caller in the call stack does not have the required permissions.

Applies to

ReceiveFromAsync(Memory<Byte>, EndPoint, CancellationToken)

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

Receives data and returns the endpoint of the sending host.

public System.Threading.Tasks.ValueTask<System.Net.Sockets.SocketReceiveFromResult> ReceiveFromAsync (Memory<byte> buffer, System.Net.EndPoint remoteEndPoint, System.Threading.CancellationToken cancellationToken = default);
member this.ReceiveFromAsync : Memory<byte> * System.Net.EndPoint * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<System.Net.Sockets.SocketReceiveFromResult>
Public Function ReceiveFromAsync (buffer As Memory(Of Byte), remoteEndPoint As EndPoint, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of SocketReceiveFromResult)

Parameters

buffer
Memory<Byte>

The buffer for the received data.

remoteEndPoint
EndPoint

An endpoint of the same type as the endpoint of the remote host.

cancellationToken
CancellationToken

A cancellation token that can be used to signal the asynchronous operation should be canceled.

Returns

An asynchronous task that completes with a SocketReceiveFromResult containing the number of bytes received and the endpoint of the sending host.

Exceptions

remoteEndPoint is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

A caller in the call stack does not have the required permissions.

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

Applies to

ReceiveFromAsync(Memory<Byte>, SocketFlags, EndPoint, CancellationToken)

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

Receives data and returns the endpoint of the sending host.

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

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.

remoteEndPoint
EndPoint

An endpoint of the same type as the endpoint of the remote host.

cancellationToken
CancellationToken

A cancellation token that can be used to signal the asynchronous operation should be canceled.

Returns

An asynchronous task that completes with a SocketReceiveFromResult containing the number of bytes received and the endpoint of the sending host.

Exceptions

remoteEndPoint is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

A caller in the call stack does not have the required permissions.

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

Applies to

ReceiveFromAsync(Memory<Byte>, SocketFlags, SocketAddress, CancellationToken)

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

Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

public System.Threading.Tasks.ValueTask<int> ReceiveFromAsync (Memory<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.SocketAddress receivedAddress, System.Threading.CancellationToken cancellationToken = default);
member this.ReceiveFromAsync : Memory<byte> * System.Net.Sockets.SocketFlags * System.Net.SocketAddress * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function ReceiveFromAsync (buffer As Memory(Of Byte), socketFlags As SocketFlags, receivedAddress As SocketAddress, 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 the SocketFlags values that will be used when receiving the data.

receivedAddress
SocketAddress

A SocketAddress instance that gets updated with the value of the remote peer when this method returns.

cancellationToken
CancellationToken

A cancellation token that can be used to signal the asynchronous operation should be canceled.

Returns

An asynchronous task that completes with a SocketReceiveFromResult containing the number of bytes received and the endpoint of the sending host.

Exceptions

receivedAddress is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

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

Applies to