This documentation is archived and is not being maintained.

Socket.Receive Method (IList<ArraySegment<Byte>>, SocketFlags)

Receives data from a bound Socket into the list of receive buffers, using the specified SocketFlags.

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

public int Receive(
	IList<ArraySegment<byte>> buffers,
	SocketFlags socketFlags


Type: System.Collections.Generic.IList<ArraySegment<Byte>>
A list of ArraySegment<T>s of type Byte that contains the received data.
Type: System.Net.Sockets.SocketFlags
A bitwise combination of the SocketFlags values.

Return Value

Type: System.Int32
The number of bytes received.


buffers is null.


buffers.Count is zero.


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


The Socket has been closed.

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

This overload requires you to provide one or more receive buffers. The SocketFlags value defaults to None.

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 connection 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, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Receive call throws a SocketException. If you are in non-blocking mode, and there is no data available in the 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 the 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 buffers parameter, buffers gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.


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.


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

The following code example demonstrates how to receive data on a connected Socket.

// Build the buffers for the receive.
List<ArraySegment<byte>> recvBuffers = 
                         new List<ArraySegment<byte>>(2);

byte[] bigBuffer = new byte[1024];

// Specify the first buffer segment (2 bytes, starting 
// at the 4th element of bigBuffer)
recvBuffers.Add(new ArraySegment<byte>
                        (bigBuffer, 4, 2));

// Specify the second buffer segment (500 bytes, starting
// at the 20th element of bigBuffer)
recvBuffers.Add(new ArraySegment<byte>
                        (bigBuffer, 20, 500));

int bytesReceived = mySocket.Receive(recvBuffers);

Console.WriteLine("{0}", ASCII.GetString(bigBuffer));

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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