System.Net.Sockets.Socket.BeginReceive Method

Begins to asynchronously receive data from a connected System.Net.Sockets.Socket.

Syntax

public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state)

Parameters

socket_flags
Documentation for this section has not yet been entered.
buffer
An array of type byte that is the storage location for the received data.
offset
The zero-based position in the buffer parameter at which to store the received data.
size
The number of bytes to receive.
callback
An AsyncCallback delegate that references the method to invoke when the operation is complete.
state
A user-defined object that contains information about the receive operation. This object is passed to the Socket.EndReceive(IAsyncResult) delegate when the operation is complete.

Returns

An IAsyncResult that references the asynchronous read.

Exceptions

TypeReason
ArgumentNullException buffer is null.
ArgumentOutOfRangeException

offset < 0.

-or-

offset > buffer.Length.

-or-

size < 0.

-or-

size > buffer.Length - offset.

System.Net.Sockets.SocketException

socketFlags is not a valid combination of values.

-or-

An error occurred while accessing the socket.

Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.

ObjectDisposedExceptionThe current instance has been disposed.

Remarks

The asynchronous Socket.BeginReceive(Byte[], int, int, SocketFlags, AsyncCallback, object) operation must be completed by calling the Socket.EndReceive(IAsyncResult) method. Typically, the method is invoked by the callback delegate.

This method does not block until the operation is complete. To block until the operation is complete, use one of the erload:System.Net.Sockets.Socket.Receive method overloads.

To cancel a pending erload:System.Net.Sockets.Socket.BeginReceive, call the Socket.Close method.

For detailed information about using the asynchronous programming model, see Asynchronous Programming Overview.

Note:

If you receive a System.Net.Sockets.SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

Note:

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

Note:

state is an instantiation of a user-defined class.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Note:

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

Example

For an outline of an asynchronous operation, see the Socket.BeginAccept(AsyncCallback, object) method. For the complete example, which uses the Socket.BeginReceive(Byte[], int, int, SocketFlags, AsyncCallback, object) method, see the System.Net.Sockets.Socket class overview.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0