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

◆ send() [1/5]

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

Sends data to a connected xtd::net::sockets::socket.

Parameters
bufferAn array of type byte that contains the data to be sent.
Returns
The number of bytes sent to the xtd::net::sockets::socket.
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
xtd::net::sockets::socket::send synchronously sends data to the remote host specified in the xtd::net::sockets::socket::connect or xtd::net::sockets::socket::accept method and returns the number of bytes successfully sent. xtd::net::sockets::socket::send can be used for both connection-oriented and connectionless protocols.
This overload requires a buffer that contains the data you want to send. The xtd::net::sockets::socket_flags value defaults to xtd::net::sockets::socket_flags::none, the buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer.
If you are using a connectionless protocol, you must call xtd::net::sockets::socket::connect before calling this method, or xtd::net::sockets::socket::send will throw a xtd::net::sockets::socket_exception. If you are using a connection-oriented protocol, you must either use xtd::net::sockets::socket::connect to establish a remote host connection, or use xtd::net::sockets::socket::accept to accept an incoming connection.
If you are using a connectionless protocol and plan to send data to several different hosts, you should use the xtd::net::sockets::socket::send_to method. If you do not use the xtd::net::sockets::socket::send_to method, you will have to call xtd::net::sockets::socket::connect before each call to xtd::net::sockets::socket::send. You can use xtd::net::sockets::socket::send_to even after you have established a default remote host with xtd::net::sockets::socket::connect. You can also change the default remote host prior to calling xtd::net::sockets::socket::send by making another call to xtd::net::sockets::socket::connect.
If you are using a connection-oriented protocol, xtd::net::sockets::socket::send will block until all of the bytes in the buffer are sent, unless a time-out was set by using xtd::net::sockets::socket::send_timeout. If the time-out value was exceeded, the xtd::net::sockets::socket::send call will throw a xtd::net::sockets::socket_exception. In nonblocking mode, xtd::net::sockets::socket::send may complete successfully even if it sends less than the number of 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 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 Send method means that the underlying system has had room to buffer your data for a network send.
Note
The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.
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.
Examples
socket_tcp_ip_v4_without_thread.cpp.