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

◆ receive() [1/5]

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

Receives data from a bound xtd::net::sockets::socket into a receive buffer.

Parameters
bufferAn array of type byte that is the storage location for the received data.
Returns
The number of bytes received.
Exceptions
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.
This overload only requires you to provide a receive buffer. The buffer offset defaults to 0, the size defaults to the size of the buffer parameter, and the xtd::net::sockets::socket_flags value defaults to xtd::net::sockets::socket_flag::none.
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.
Examples
socket_tcp_ip_v4_without_thread.cpp.