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

◆ send_to() [1/4]

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

Sends data to the specified endpoint.

Parameters
bufferAn array of type byte that contains the data to be sent.
remote_end_pointThe xtd::net::end_point that represents the destination for the data.
Returns
The number of bytes sent.
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
In this overload, the buffer offset defaults to 0, the number of bytes to send defaults to the size of the buffer parameter, and the xtd::net::sockets::socket_flags value defaults to xtd::net::sockets::socket_flags::none.
If you are using a connectionless protocol, you do not need to establish a default remote host with the xtd::net::sockets::socket::connect method prior to calling xtd::net::sockets::socket::send_to. You only need to do this if you intend to call the xtd::net::sockets::socket::send method. If you do call the xtd::net::sockets::socket::connect method prior to calling xtd::net::sockets::socket::send_tto, the remote_end_point parameter will override the specified default remote host for that send operation only. You are also not required to call the xtd::net::sockets::socket::bind method, because the underlying service provider will assign the most appropriate local network address and port number. If you need to identify the assigned local network address and port number, you can use the xtd::net::sockets::socket::local_end_point property after the xtd::net::sockets::socket::send_to method successfully completes.
Although intended for connectionless protocols, xtd::net::sockets::socket::send_to also works with connection-oriented protocols. If you are using a connection-oriented protocol, you must first establish a remote host connection by calling the xtd::net::sockets::socket::connect method or accept an incoming connection request using the xtd::net::sockets::socket::accept method. If you do not establish or accept a remote host connection, xtd::net::sockets::socket::send_to will throw a xtd::net::sockets::socket_exception. You can also establish a default remote host for a connectionless protocol prior to calling the xtd::net::sockets::socket::send_to method. In either of these cases, xtd::net::sockets::socket::send_to will ignore the remote_end_point parameter and only send data to the connected or default remote host.
Blocking sockets will block until the all of the bytes in the buffer are sent. Since a nonblocking xtd::net::sockets::socket completes immediately, it might not send all of the bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends all of the bytes in the buffer. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the xtd::net::sockets::socket::send_to method means that the underlying system has had room to buffer your data for a network send.
If you are using a connectionless protocol in blocking mode, xtd::net::sockets::socket::send_to will block until the datagram is sent. If you want to send data to a broadcast address, you must first call the xtd::net::sockets::socket::set_socket_option method and set the socket option to xtd::net::sockets::socket_option_name::broadcast. You must also be sure that the number of bytes sent does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and xtd::net::sockets::socket::send_to will throw a xtd::net::sockets::socket_exception.
Note
If you receive a xtd::net::sockets::socket_exception 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.