Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Socket.Receive Method (Byte[], Int32, Int32, SocketFlags)

Receives the specified number of bytes from a bound Socket into the specified offset position of the receive buffer, using the specified SocketFlags.

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

public int Receive (
	byte[] buffer,
	int offset,
	int size,
	SocketFlags socketFlags
)
public int Receive (
	byte[] buffer, 
	int offset, 
	int size, 
	SocketFlags socketFlags
)
public function Receive (
	buffer : byte[], 
	offset : int, 
	size : int, 
	socketFlags : SocketFlags
) : int
Not applicable.

Parameters

buffer

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

offset

The location in buffer to store the received data.

size

The number of bytes to receive.

socketFlags

A bitwise combination of the SocketFlags values.

Return Value

The number of bytes received.

Exception typeCondition

ArgumentNullException

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

ArgumentOutOfRangeException

offset is less than 0.

-or-

offset is greater than the length of buffer.

-or-

size is less than 0.

-or-

size is greater than the length of buffer minus the value of the offset parameter.

SocketException

socketFlags is not a valid combination of values.

-or-

The LocalEndPoint property was not set.

-or-

An operating system error occurs while accessing the Socket.

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.

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, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Receive call will throw 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. An error occurred when attempting to access the socket. See Remarks below. 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 number of bytes specified by the size parameter. 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 queued 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.

The following code example specifies a data buffer, an offset, a size, and a socket flag before receiving data on a connected Socket.

// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
public static int SendReceiveTest4(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try 
    {
        // Blocks until send returns.
        int byteCount = server.Send(msg, 0, msg.Length, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", byteCount);
        
        // Get reply from the server.
        byteCount = server.Receive(bytes, 0, server.Available, 
                                   SocketFlags.None);

        if (byteCount > 0)
            Console.WriteLine(Encoding.UTF8.GetString(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, offset, message size,
// and socket flags.
public static int SendReceiveTest4(Socket server)
{
    ubyte msg[] = Encoding.get_UTF8().GetBytes("This is a test");
    ubyte bytes[] = new ubyte[256];
    try {
        // Blocks until send returns.
        int i = server.Send(msg, 0, msg.get_Length(), SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", (Int32)i);
        // Get reply from the server.
        server.Receive(bytes, 0, server.get_Available(), SocketFlags.None);
        Console.WriteLine(Encoding.get_UTF8().GetString(bytes));
    }
    catch (SocketException e) {
        Console.WriteLine("{0} Error code: {1}.", e.get_Message(),
            (Int32)e.get_ErrorCode());
        return e.get_ErrorCode();
    }
    return 0;
} //SendReceiveTest4

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft