Export (0) Print
Expand All

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

.NET Framework 1.1

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

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

Parameters

buffer
An array of type Byte that is the storage location for 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 size exceeds the size of buffer.
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.

Remarks

The Receive This 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 number of bytes you want to receive, and the necessary SocketFlags. The buffer offset defaults to 0. 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. 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 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 receives the data found into buffer, and specifies None for SocketFlags.

[Visual Basic] 

      ' Receive the host home page content and loop until all the data is received.

      'Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
      Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)

      strRetPage = "Default HTML page on " + server + ":\r\n"
      strRetPage = "Default HTML page on " + server + ":" + ControlChars.Lf + ControlChars.NewLine

      Dim i As Integer

      While bytes > 0

        bytes = s.Receive(RecvBytes, RecvBytes.Length, 0)

        strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes)

      End While


[C#] 

     // Receive the host home page content and loop until all the data is received.
     Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
     strRetPage = "Default HTML page on " + server + ":\r\n";
     strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

     while (bytes > 0)
     {
       bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
       strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
     }


[C++] 

      // Receive the host home page content and loop until all the data is received.
      Int32 bytes = s->Receive(RecvBytes, RecvBytes->Length, SocketFlags::None);
      strRetPage = "Default HTML page on ";
      strRetPage->Concat(server, ":\r\n", ASCII->GetString(RecvBytes, 0, bytes));

      while (bytes > 0)
      {
         bytes = s->Receive(RecvBytes, RecvBytes->Length, SocketFlags::None);
         strRetPage->Concat(ASCII->GetString(RecvBytes, 0, bytes));
      }

[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