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

◆ receive_from() [1/4]

size_t xtd::net::sockets::socket::receive_from ( std::vector< xtd::byte > &  buffer,
xtd::net::end_point remote_end_point 
)

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.
remote_end_pointthe remote host
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_from method reads data into the buffer parameter, returns the number of bytes successfully read, and captures the remote host endpoint from which the data was sent. This method is useful if you intend to receive connectionless datagrams from an unknown host or multiple hosts.
This overload only requires you to provide a receive buffer, and an xtd::net::end_point that represents the remote host. 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_flags::none.
Note
Before calling xtd::net::sockets::socket::receive_from, you must explicitly bind the xtd::net::sockets::socket to a local endpoint using the xtd::net::sockets::socket::bind method. If you do not, xtd::net::sockets::socket::receive_from will throw a xtd::net::sockets::socket::socket_exception.
Remarks
With connectionless protocols, xtd::net::sockets::socket::receive_from will read the first enqueued datagram received into the local network buffer. If the datagram you receive is larger than the size of buffer, the xtd::net::sockets::socket::receive_from method will fill buffer with as much of the message as is possible, and throw a xtd::net::sockets::socket::socket_exception. If you are using an unreliable protocol, the excess data will be lost. If you are using a reliable protocol, the excess data will be retained by the service provider and you can retrieve it by calling the xtd::net::sockets::socket::receive_from method with a large enough buffer.
If no data is available for reading, the xtd::net::sockets::socket::receive_from method will block until data is available. 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_from method will complete immediately and throw a xtd::net::sockets::socket::socket_exception. You can use the xtd::net::sockets::socket::socket::available property to determine if data is available for reading. When xtd::net::sockets::socket::socket::available is non-zero, retry the receive operation.
Although xtd::net::sockets::socket::receive_from is intended for connectionless protocols, you can use a connection-oriented protocol as well. If you choose to do so, you must first either establish a remote host connection by calling the xtd::net::sockets::socket::socket::connect method or accept an incoming remote host connection by calling the xtd::net::sockets::socket::socket::accept method. If you do not establish or accept a connection before calling the xtd::net::sockets::socket::receive_from method, you will get a xtd::net::sockets::socket::socket_exception. You can also establish a default remote host for a connectionless protocol prior to calling the xtd::net::sockets::socket::receive_from method. In either of these cases, the xtd::net::sockets::socket::receive_from method will ignore the remote_end_point parameter and only receive data from the connected or default remote host.
With connection-oriented sockets, xtd::net::sockets::socket::receive_from will read as much data as is available up to the size of buffer. If the remote host shuts down the xtd::net::sockets::socket connection with the xtd::net::sockets::socket::socket::shutdown method, and all available data has been received, the xtd::net::sockets::socket::receive_from method will complete immediately and return zero bytes.
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.
The xtd::net::sockets::sockets::address_family of the xtd::net::end_point used in xtd::net::sockets::socket::receive_from needs to match the xtd::net::sockets::sockets::address_family of the xtd::net::end_point used in xtd::net::sockets::sockets::socket::send_to.