Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Socket.Receive Method (Byte[])

.NET Framework 1.1

Receives data from a bound Socket into a receive buffer.

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


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

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. The buffer offset defaults to 0, the size defaults to the length of the buffer parameter, and 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 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 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 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 receives data on a connected Socket.

[Visual Basic] 
' Displays sending with a connected socket
' using the overload that takes a buffer.
Public Shared Function SendReceiveTest1(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)
        Console.WriteLine("Sent {0} bytes.", i)
        ' Get reply from the server.
        i = server.Receive(bytes)
    Catch e As SocketException
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
        Return e.ErrorCode
    End Try
    Return 0
End Function 'SendReceiveTest1

// Displays sending with a connected socket
// using the overload that takes a buffer.
public static int SendReceiveTest1(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);
        Console.WriteLine("Sent {0} bytes.", i);
        // Get reply from the server.
        i = server.Receive(bytes);
    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.
int SendReceiveTest1(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);
        Console::WriteLine(S"Sent {0} bytes.", i.ToString());
        // Get reply from the server.
        i = server->Receive(bytes);
    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

© 2015 Microsoft