xtd 0.2.0
Loading...
Searching...
No Matches

◆ receive() [4/5]

size_t xtd::net::sockets::socket::receive ( std::vector< xtd::byte > &  buffer,
size_t  offset,
size_t  size,
xtd::net::sockets::socket_flags  socket_flags 
)

Receives the specified number of bytes from a bound xtd::net::sockets::socket into the specified offset position of the receive buffer, using the specified xtd::net::sockets::socket_flags.

Parameters
bufferAn array of type byte that is the storage location for the received data.
offsetThe location in buffer to store the received data.
sizeThe number of bytes to receive.
socket_flagsA bitwise combination of the xtd::net::sockets::socket_flags values.
Returns
The number of bytes received.
Exceptions
xtd::argument_out_of_range_exceptionsize is less than 0 or exceeds the size of the buffer.
xtd::net::sockets::socket_exceptionAn error occurred when attempting to access the socket.
xtd::object_closed_exceptionThe xtd::net::sockets::socket has been closed.
Remarks
The xtd::net::sockets::socket::receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call xtd::net::sockets::socket::receive from both connection-oriented and connectionless sockets.
If you are using a connection-oriented protocol, you must either call xtd::net::sockets::socket::connect to establish a remote host connection, or xtd::net::sockets::socket::accept to accept an incoming connection prior to calling xtd::net::sockets::socket::receive. The xtd::net::sockets::socket::receive method will only read data that arrives from the remote host established in the xtd::net::sockets::socket::connect or xtd::net::sockets::socket::accept method. If you are using a connectionless protocol, you can also use the xtd::net::sockets::socket::receive_from method. xtd::net::sockets::socket::receive_from will allow you to receive data arriving from any host.
If no data is available for reading, the xtd::net::sockets::socket::receive method will block until data is available, unless a time-out value was set by using xtd::net::sockets::socket::receive_timeout. If the time-out value was exceeded, the xtd::net::sockets::socket::receive call will throw a xtd::net::sockets::socket_exception. If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the xtd::net::sockets::socket::receive method will complete immediately and throw a xtd::net::sockets::socket_exception. You can use the xtd::net::sockets::socket::available property to determine if data is available for reading. When xtd::net::sockets::socket::available is non-zero, retry the receive operation.
If you are using a connection-oriented xtd::net::sockets::socket, the xtd::net::sockets::socket::receive method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the xtd::net::sockets::socket connection with the xtd::net::sockets::socket::shutdown method, and all available data has been received, the xtd::net::sockets::socket::receive method will complete immediately and return zero bytes.
If you are using a connectionless xtd::net::sockets::socket, xtd::net::sockets::socket::receive will read the first queued datagram from the destination address you specify in the xtd::net::sockets::socket::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 xtd::net::sockets::socket::socket_exception is thrown.
Note
If you receive a xtd::net::sockets::socket_exception, use the xtd::net::sockets::socket_exception::error_code 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.