System.Net.Sockets.Socket.Receive Method

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

Syntax

public int Receive (byte[] buffer, 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 the received data.
size
The number of bytes to receive.

Returns

The number of bytes received.

Exceptions

TypeReason
ArgumentNullException buffer is null.
ArgumentOutOfRangeException

size < 0.

-or-

size > buffer.Length.

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-

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.

This overload only requires you to provide a receive buffer, the number of bytes you want to receive, and the necessary System.Net.Sockets.SocketFlags.

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. You can use the Socket.Available property to determine if data is available for reading. When Socket.Available is non-zero, retry your 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 connections. 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