This documentation is archived and is not being maintained.

Socket.Receive Method (Byte[], SocketFlags)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Function Receive( _
   ByVal buffer() As Byte, _
   ByVal socketFlags As SocketFlags _
) As Integer
public int Receive(
 byte[] buffer,
 SocketFlags socketFlags
public: int Receive(
 unsigned char buffer __gc[],
 SocketFlags socketFlags
public function Receive(
   buffer : Byte[],
 socketFlags : SocketFlags
) : int;


An array of type Byte that is the storage location for the received data.
A bitwise combination of the SocketFlags values.

Return Value

The number of bytes received.


Exception Type Condition
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 specify the Peek flag as the socketFlags parameter, the available data is copied into the receive buffer but not removed from the system buffer. If you specify the OutOfBand flag as the socketFlags parameter and the Socket is configured for in-line reception of out-of-band (OOB) data (using OutOfBandInline) and OOB data is available, Receive will return only OOB data. The OOB data is a logically independent transmission channel associated with each pair of connected stream sockets. This data is delivered with higher priority and independently of the normal data.

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.

Note   If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.


[Visual Basic, C#, C++] The following example specifies a data buffer, and SocketFlags for receiving data on a connected Socket.

[Visual Basic] 
' Displays sending with a connected socket
' using the overload that takes a buffer and socket flags.
Public Shared Function SendReceiveTest2(server As Socket) As Integer
    Dim msg As Byte() = Encoding.UTF8.GetBytes("This is a test")
    Dim bytes(255) As Byte
        ' Blocks until send returns.
        Dim i As Integer = server.Send(msg, SocketFlags.None)
        Console.WriteLine("Sent {0} bytes.", i)
        ' Get reply from the server.
        i = server.Receive(bytes, SocketFlags.None)
    Catch e As SocketException
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
        Return e.ErrorCode
    End Try
    Return 0
End Function 'SendReceiveTest2

// Displays sending with a connected socket
// using the overload that takes a buffer and socket flags.
public static int SendReceiveTest2(Socket server)
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
        // Blocks until send returns.
        int i = server.Send(msg, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", i);
        // Get reply from the server.
        i = server.Receive(bytes, SocketFlags.None);
    catch (SocketException e)
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    return 0;

// Displays sending with a connected socket
// using the overload that takes a buffer and socket flags.
int SendReceiveTest2(Socket* server)
    Byte msg[]= Encoding::UTF8->GetBytes(S"This is a test");
    Byte bytes[] = new Byte[256];
        // Blocks until send returns.
        int i = server->Send(msg, SocketFlags::None);
        Console::WriteLine(S"Sent {0} bytes.", i.ToString());
        // Get reply from the server.
        i = server->Receive(bytes, SocketFlags::None);
    catch (SocketException* e)
        Console::WriteLine(S"{0} Error code: {1}.", e->Message, e->ErrorCode.ToString());
        return (e->ErrorCode);
    return 0;

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace | Socket.Receive Overload List | Connect | ReceiveFrom | Available | Shutdown | Close