System.Net.Sockets.Socket.Receive Method

Receives 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, 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.

Returns

The number of bytes received.

Exceptions

TypeReason
ArgumentNullException buffer is null.
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.SecurityException A caller in the call stack does not have the required permissions.
ObjectDisposedExceptionThe current instance has been disposed.

Remarks

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

This overload only requires you to provide a receive buffer and the necessary System.Net.Sockets.SocketFlags. The buffer offset defaults to 0, and the size defaults to the length of the byte parameter.

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[], SocketFlags). The Socket.Receive(Byte[], 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[], SocketFlags) method will block until data is available. If you are in non-blocking mode, and there is no data available in the protocol stack buffer, the Socket.Receive(Byte[], 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[], SocketFlags) method will read as much data as is available up to the size of the buffer. 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[], SocketFlags) method will complete immediately and return zero bytes.

If you are using a connectionless System.Net.Sockets.Socket, Socket.Receive(Byte[], SocketFlags) will read the first enqueued 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.

Requirements

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