संपादित करें

इसके माध्यम से साझा किया गया


Socket.BeginReceiveFrom Method

Definition

Begins to asynchronously receive data from a specified network device.

public:
 IAsyncResult ^ BeginReceiveFrom(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ % remoteEP, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginReceiveFrom (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP, AsyncCallback? callback, object? state);
public IAsyncResult BeginReceiveFrom (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP, AsyncCallback callback, object state);
member this.BeginReceiveFrom : byte[] * int * int * System.Net.Sockets.SocketFlags * EndPoint * AsyncCallback * obj -> IAsyncResult
Public Function BeginReceiveFrom (buffer As Byte(), offset As Integer, size As Integer, socketFlags As SocketFlags, ByRef remoteEP As EndPoint, callback As AsyncCallback, state As Object) As IAsyncResult

Parameters

buffer
Byte[]

An array of type Byte that is the storage location for the received data.

offset
Int32

The zero-based position in the buffer parameter at which to store the data.

size
Int32

The number of bytes to receive.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
EndPoint

A reference to an EndPoint of the same type as the endpoint of the remote host to be updated on synchronous receive.

callback
AsyncCallback

The AsyncCallback delegate.

state
Object

An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous read.

Exceptions

buffer is null.

-or-

remoteEP is null.

.NET Framework and .NET 5 and earlier only: An error occurred when attempting to access the socket.

offset is less than 0.

-or-

offset is greater than the length of buffer.

-or-

size is less than 0.

-or-

size is greater than the length of buffer minus the value of the offset parameter.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

Remarks

Important

This is a compatibility API. We don't recommend using the APM (Begin* and End*) methods for new development. Instead, use the Task-based equivalents.

You can pass a callback that implements AsyncCallback to BeginReceiveFrom in order to get notified about the completion of the operation. Note that if the underlying network stack completes the operation synchronously, the callback will be executed inline, during the call to BeginReceiveFrom. In this case, the CompletedSynchronously property on the returned IAsyncResult will be set to true to indicate that the method completed synchronously. Use the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginReceiveFrom method.

The asynchronous BeginReceiveFrom operation must be completed by calling the EndReceiveFrom method. Typically, the method is invoked by the AsyncCallback delegate. EndReceiveFrom will block the calling thread until the operation is completed.

This method reads data into the buffer parameter, and captures the remote host endpoint from which the data is sent. For information on how to retrieve this endpoint, refer to EndReceiveFrom. This method is most useful if you intend to asynchronously receive connectionless datagrams from an unknown host or multiple hosts. In these cases, BeginReceiveFrom will read the first enqueued datagram received into the local network buffer. If the datagram you receive is larger than the size of buffer, the BeginReceiveFrom method will fill buffer with as much of the message as is possible, and throw a SocketException. If you are using an unreliable protocol, the excess data will be lost. If you are using a reliable protocol, the excess data will be retained by the service provider and you can retrieve it by calling the BeginReceiveFrom method with a large enough buffer.

To guarantee that the remote host endpoint is always returned, an application should explicitly bind the Socket to a local endpoint using the Bind method and then call the SetSocketOption method with the optionLevel parameter set to IP or IPv6 as appropriate, the optionName parameter set to PacketInformation, and the optionValue parameter to enable this option before calling the BeginReceiveFrom method. Otherwise, it is possible for the remote host endpoint to not be returned when the sender has sent a number of datagrams before the receiver has called the BeginReceiveFrom method.

Although BeginReceiveFrom is intended for connectionless protocols, you can use a connection-oriented protocol as well. If you choose to do so, you must first either establish a remote host connection by calling the Connect / BeginConnect method or accept an incoming connection request by calling the Accept or BeginAccept method. If you call the BeginReceiveFrom method before establishing or accepting a connection, you will get a SocketException. You can also establish a default remote host for a connectionless protocol prior to calling the BeginReceiveFrom method.

The remoteEp parameter

With connection-oriented sockets, BeginReceiveFrom will read as much data as is available up to the number of bytes specified by the size parameter.

To cancel a pending BeginReceiveFrom, call the Close method.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code.

Note

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

Note

The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. After the first use of a particular context (a specific asynchronous Socket method, a specific Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

Applies to

See also