Socket.Receive Method (Byte, Int32, SocketFlags)
Assembly: System (in System.dll)
The number of bytes to receive.
Return ValueType: System.Int32
The number of bytes received.
buffer is null.
size exceeds the size of buffer.
An error occurred when attempting to access the socket. See the Remarks section for more information.
The Socket has been closed.
A caller in the call stack does not have the required permissions.
Themethod reads data into the buffer parameter and returns the number of bytes successfully read. You can call 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.
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 . The 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 Socket.ReceiveTimeout. If the time-out value was exceeded, the 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 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.method will block until data is available, unless a time-out value was set by using
If you are using a connection-oriented Socket, the 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 method will complete immediately and return zero bytes.
If you are using a connectionless Socket, 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 in the .NET Framework.
Available since 1.1