Export (0) Print
Expand All

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

.NET Framework 1.1

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

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

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.

Exceptions

Exception Type Condition
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.

Remarks

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 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 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 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.

Example

[Visual Basic, C#, C++] The following example specifies a data buffer, an offset, a size, and a socket flag before receiving data on a connected Socket.

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


[C#] 
// 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 i = server.Send(msg, 0, msg.Length, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", i);
        
        // Get reply from the server.
        server.Receive(bytes, 0, server.Available, SocketFlags.None);
        Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}

[C++] 
// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
int SendReceiveTest4(Socket* server)
{
    Byte msg[]= Encoding::UTF8->GetBytes(S"This is a test");
    Byte bytes[] = new Byte[256];
    try 
    {
        // Blocks until send returns.
        int i = server->Send(msg, 0, msg.Length, SocketFlags::None);
        Console::WriteLine(S"Sent {0} bytes.", i.ToString());
        
        // Get reply from the server.
        server->Receive(bytes, 0, server->Available, SocketFlags::None);
        Console::WriteLine(Encoding::UTF8->GetString(bytes));
    }
    catch (SocketException* e)
    {
        Console::WriteLine("{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.

Requirements

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

Show:
© 2014 Microsoft