Socket.Receive Method (Byte[], SocketFlags)

Receives data from a bound Socket into a receive buffer, using the specified SocketFlags.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

member Receive : 
        buffer:byte[] * 
        socketFlags:SocketFlags -> int

Parameters

buffer
Type: System.Byte[]

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

socketFlags
Type: System.Net.Sockets.SocketFlags

A bitwise combination of the SocketFlags values.

Return Value

Type: System.Int32
The number of bytes received.

ExceptionCondition
ArgumentNullException

buffer is a null reference (Nothing in Visual Basic).

SocketException

An error occurred when attempting to access the socket. See the Remarks section for more information.

ObjectDisposedException

The Socket has been closed.

SecurityException

A caller in the call stack does not have the required permissions.

The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

This overload only requires you to provide a receive buffer and the necessary 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 Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive 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 Receive method will complete immediately and throw a SocketException. You can use the Available property to determine if data is available for reading. When Available is non-zero, retry your receive operation.

If you are using a connection-oriented Socket, the Receive method will read as much data as is available up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

If you are using a connectionless Socket, Receive will read the first enqueued datagram from the destination address you specify in the Connect 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 SocketException is thrown.

NoteNote

If you receive a 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.

NoteNote

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

The following code example specifies a data buffer, and SocketFlags for receiving data on a connected Socket.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft