Socket.Receive Method (Byte, Int32, Int32, SocketFlags, SocketError)
Assembly: System (in system.dll)
public int Receive ( byte buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode )
public int Receive ( byte buffer, int offset, int size, SocketFlags socketFlags, /** @attribute OutAttribute() */ /** @ref */ SocketError errorCode )
An array of type Byte that is the storage location for the received data.
The position in the buffer parameter to store the received data.
The number of bytes to receive.
A bitwise combination of the SocketFlags values.
A SocketError object that stores the socket error.
Return ValueThe number of bytes received.
buffer is a null reference (Nothing in Visual Basic).
offset is less than 0.
offset is greater than the length of buffer.
size is less than 0.
size is greater than the length of buffer minus the value of the offset parameter.
socketFlags is not a valid combination of values.
The LocalEndPoint property is not set.
An operating system error occurs while accessing the Socket.
The Socket has been closed.
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 ReceiveThe 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.
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.
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.