Receives data from a bound System.Net.Sockets.Socket into a receive buffer.
- buffer
- An array of type byte that is the storage location for the received data.
The number of bytes received.
Type Reason ArgumentNullException buffer is null. InvalidOperationException An asynchronous call is pending and a blocking method has been called. System.Net.Sockets.SocketException An error occurred while accessing the socket.
Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.System.Security.SecurityException A caller in the call stack does not have the required permissions. ObjectDisposedException The current instance has been disposed.
The Socket.Receive(Byte[]) method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Socket.Receive(Byte[]) from both connection-oriented and connectionless sockets.
This overload only requires you to provide a receive buffer. The buffer offset defaults to 0, the size defaults to the length of the buffer parameter, and the System.Net.Sockets.SocketFlags value defaults to SocketFlags.None.
If you are using a connection-oriented protocol, you must either call Socket.Connect(System.Net.EndPoint) to establish a remote host connection, or Socket.Accept to accept an incoming connection prior to calling Socket.Receive(Byte[]). The Socket.Receive(Byte[]) method will only read data that arrives from the remote host established in the Socket.Connect(System.Net.EndPoint) or Socket.Accept method. If you are using a connectionless protocol, you can also use the Socket.ReceiveFrom(Byte[], int, int, SocketFlags, System.Net.EndPoint@) method. Socket.ReceiveFrom(Byte[], int, int, SocketFlags, System.Net.EndPoint@) will allow you to receive data arriving from any host.
If no data is available for reading, the Socket.Receive(Byte[]) 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 Socket.Receive(Byte[]) call will throw a System.Net.Sockets.SocketException. If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Socket.Receive(Byte[]) method will complete immediately and throw a System.Net.Sockets.SocketException. You can use the Socket.Available property to determine if data is available for reading. When Socket.Available is non-zero, retry the receive operation.
If you are using a connection-oriented System.Net.Sockets.Socket, the Socket.Receive(Byte[]) method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the System.Net.Sockets.Socket connection with the Socket.Shutdown(SocketShutdown) method, and all available data has been received, the Socket.Receive(Byte[]) method will complete immediately and return zero bytes.
If you are using a connectionless System.Net.Sockets.Socket, Socket.Receive(Byte[]) will read the first queued datagram from the destination address you specify in the Socket.Connect(System.Net.EndPoint) 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 System.Net.Sockets.SocketException is thrown.
If you receive a System.Net.Sockets.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 [<topic://conUsingNetworkTracing>].
Type | Reason |
---|---|
System.Net.SocketPermission | Requires permission to accept connections. See System.Net.NetworkAccess.Accept. |