Bewerken

Share via


Socket.ReceiveMessageFromAsync Method

Definition

Overloads

ReceiveMessageFromAsync(SocketAsyncEventArgs)

Begins to asynchronously receive the specified number of bytes of data into the specified location in the data buffer, using the specified SocketFlags, and stores the endpoint and packet information.

ReceiveMessageFromAsync(ArraySegment<Byte>, EndPoint)

Receives data and returns additional information about the sender of the message.

ReceiveMessageFromAsync(ArraySegment<Byte>, SocketFlags, EndPoint)

Receives data and returns additional information about the sender of the message.

ReceiveMessageFromAsync(Memory<Byte>, EndPoint, CancellationToken)

Receives data and returns additional information about the sender of the message.

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

Receives data and returns additional information about the sender of the message.

ReceiveMessageFromAsync(SocketAsyncEventArgs)

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

Begins to asynchronously receive the specified number of bytes of data into the specified location in the data buffer, using the specified SocketFlags, and stores the endpoint and packet information.

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

The Socket has been closed.

An error occurred when attempting to access the socket.

Remarks

The ReceiveMessageFromAsync method is used primarily to receive message data on a connectionless socket. The socket's local address must be known. This method can only be used with datagram and raw sockets. The socket must be initialized with the socket type set to Dgram or Raw before calling this method. This can be done when the socket is constructed using Socket.

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 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 ReceiveMessageFromAsync 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.

The ReceiveMessageFromAsync method automatically sets the PacketInformation socket option to true the first time it is called for a given Socket. However, the IPPacketInformation object will only be valid for packets which arrive at the local computer after the socket option has been set. If a socket is sent packets between when the socket is bound to a local endpoint (explicitly by the Bind method or implicitly by one of the Connect, ConnectAsync, SendTo, or SendToAsync methods) and the first call to the ReceiveMessageFromAsync method, calls to ReceiveMessageFromAsync method will result in invalid IPPacketInformation objects for these packets.

To ensure that all IPPacketInformation objects are valid, an application should set the PacketInformation socket option totrue before it is bound to a local endpoint using the SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) method.

An application can examine the resulting IPPacketInformation objects if it needs to know if the datagram was sent using a unicast, multicast, or broadcast address.

See also

Applies to

ReceiveMessageFromAsync(ArraySegment<Byte>, EndPoint)

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

Receives data and returns additional information about the sender of the message.

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

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 SocketReceiveMessageFromResult containing the number of bytes received and additional information about the sending host.

Exceptions

The Socket object has been closed.

remoteEndPoint is null.

You must call the Bind method before performing this operation.

Applies to

ReceiveMessageFromAsync(ArraySegment<Byte>, SocketFlags, EndPoint)

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

Receives data and returns additional information about the sender of the message.

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

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 SocketReceiveMessageFromResult containing the number of bytes received and additional information about the sending host.

Exceptions

The Socket object has been closed.

remoteEndPoint is null.

You must call the Bind method before performing this operation.

Applies to

ReceiveMessageFromAsync(Memory<Byte>, EndPoint, CancellationToken)

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

Receives data and returns additional information about the sender of the message.

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

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 SocketReceiveMessageFromResult containing the number of bytes received and additional information about the sending host.

Exceptions

The Socket object has been closed.

remoteEndPoint is null.

You must call the Bind method before performing this operation.

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

Applies to

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

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

Receives data and returns additional information about the sender of the message.

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

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 SocketReceiveMessageFromResult containing the number of bytes received and additional information about the sending host.

Exceptions

The Socket object has been closed.

remoteEndPoint is null.

You must call the Bind method before performing this operation.

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

Applies to