System.Net.Sockets.Socket.Receive Method

Receives the specified number of bytes from a bound System.Net.Sockets.Socket into the specified offset position of the receive buffer, using the specified System.Net.Sockets.SocketFlags.

Syntax

public int Receive (byte[] buffer, int offset, int size, SocketFlags flags)

Parameters

flags
Documentation for this section has not yet been entered.
buffer
An array of type byte that is the storage location for received data.
offset
The location in buffer to store the received data.
size
The number of bytes to receive.

Returns

The number of bytes received.

Exceptions

TypeReason
ArgumentNullException buffer is null.
ArgumentOutOfRangeException

offset < 0.

-or-

offset > buffer.Length.

-or-

size < 0.

-or-

size > buffer.Length - offset.

InvalidOperationExceptionAn asynchronous call is pending and a blocking method has been called.
System.Net.Sockets.SocketException

socketFlags is not a valid combination of values.

-or-

The Socket.LocalEndPoint property was not set.

-or-

An error occurred while accessing the socket.

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

System.Security.SecurityExceptionA caller in the call stack does not have the required permissions.
ObjectDisposedExceptionThe current instance has been disposed.

Remarks

The Socket.Receive(Byte[], int, SocketFlags) method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Socket.Receive(Byte[], int, SocketFlags) from both connection-oriented and connectionless sockets.

If you are using a connection-oriented protocol, you must either call Socket.Connect(System.Net.EndPoint) to establish a remote host connection, or Socket.Accept to accept an incoming connection prior to calling Socket.Receive(Byte[], int, SocketFlags). The Socket.Receive(Byte[], int, SocketFlags) method will only read data that arrives from the remote host established in the Socket.Connect(System.Net.EndPoint) or Socket.Accept method. If you are using a connectionless protocol, you can also use the Socket.ReceiveFrom(Byte[], int, int, SocketFlags, System.Net.EndPoint@) method. Socket.ReceiveFrom(Byte[], int, int, SocketFlags, System.Net.EndPoint@) will allow you to receive data arriving from any host.

If no data is available for reading, the Socket.Receive(Byte[], int, SocketFlags) method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Socket.Receive(Byte[], int, SocketFlags) call will throw a System.Net.Sockets.SocketException. If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Socket.Receive(Byte[], int, SocketFlags) method will complete immediately and throw a System.Net.Sockets.SocketException. An error occurred when attempting to access the socket. See Remarks below. You can use the Socket.Available property to determine if data is available for reading. When Socket.Available is non-zero, retry the receive operation.

If you are using a connection-oriented System.Net.Sockets.Socket, the Socket.Receive(Byte[], int, SocketFlags) method will read as much data as is available, up to the number of bytes specified by the size parameter. If the remote host shuts down the System.Net.Sockets.Socket connection with the Socket.Shutdown(SocketShutdown) method, and all available data has been received, the Socket.Receive(Byte[], int, SocketFlags) method will complete immediately and return zero bytes.

If you are using a connectionless System.Net.Sockets.Socket, Socket.Receive(Byte[], int, SocketFlags) will read the first queued datagram from the destination address you specify in the Socket.Connect(System.Net.EndPoint) method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a System.Net.Sockets.SocketException is thrown.

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:

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

Permissions

TypeReason
System.Net.SocketPermissionRequires permission to accept a connection on the endpoint defined by the Socket.LocalEndPoint property of the current instance. See System.Net.NetworkAccess.Accept.

Requirements

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