The class template basic_datagram_socket<Protocol> is used to send and receive discrete messages of fixed maximum length.
namespace std { namespace experimental { namespace net { inline namespace v1 { template<class Protocol> class basic_datagram_socket : public basic_socket<Protocol> { public: // types: using native_handle_type = implementation-defined; // see [socket.reqmts.native] using protocol_type = Protocol; using endpoint_type = typename protocol_type::endpoint; // [socket.dgram.cons], construct / copy / destroy: explicit basic_datagram_socket(io_context& ctx); basic_datagram_socket(io_context& ctx, const protocol_type& protocol); basic_datagram_socket(io_context& ctx, const endpoint_type& endpoint); basic_datagram_socket(io_context& ctx, const protocol_type& protocol, const native_handle_type& native_socket); basic_datagram_socket(const basic_datagram_socket&) = delete; basic_datagram_socket(basic_datagram_socket&& rhs); template<class OtherProtocol> basic_datagram_socket(basic_datagram_socket<OtherProtocol>&& rhs); ~basic_datagram_socket(); basic_datagram_socket& operator=(const basic_datagram_socket&) = delete; basic_datagram_socket& operator=(basic_datagram_socket&& rhs); template<class OtherProtocol> basic_datagram_socket& operator=(basic_datagram_socket<OtherProtocol>&& rhs); // [socket.dgram.op], basic_datagram_socket operations: template<class MutableBufferSequence> size_t receive(const MutableBufferSequence& buffers); template<class MutableBufferSequence> size_t receive(const MutableBufferSequence& buffers, error_code& ec); template<class MutableBufferSequence> size_t receive(const MutableBufferSequence& buffers, socket_base::message_flags flags); template<class MutableBufferSequence> size_t receive(const MutableBufferSequence& buffers, socket_base::message_flags flags, error_code& ec); template<class MutableBufferSequence, class CompletionToken> DEDUCED async_receive(const MutableBufferSequence& buffers, CompletionToken&& token); template<class MutableBufferSequence, class CompletionToken> DEDUCED async_receive(const MutableBufferSequence& buffers, socket_base::message_flags flags, CompletionToken&& token); template<class MutableBufferSequence> size_t receive_from(const MutableBufferSequence& buffers, endpoint_type& sender); template<class MutableBufferSequence> size_t receive_from(const MutableBufferSequence& buffers, endpoint_type& sender, error_code& ec); template<class MutableBufferSequence> size_t receive_from(const MutableBufferSequence& buffers, endpoint_type& sender, socket_base::message_flags flags); template<class MutableBufferSequence> size_t receive_from(const MutableBufferSequence& buffers, endpoint_type& sender, socket_base::message_flags flags, error_code& ec); template<class MutableBufferSequence, class CompletionToken> DEDUCED async_receive_from(const MutableBufferSequence& buffers, endpoint_type& sender, CompletionToken&& token); template<class MutableBufferSequence, class CompletionToken> DEDUCED async_receive_from(const MutableBufferSequence& buffers, endpoint_type& sender, socket_base::message_flags flags, CompletionToken&& token); template<class ConstBufferSequence> size_t send(const ConstBufferSequence& buffers); template<class ConstBufferSequence> size_t send(const ConstBufferSequence& buffers, error_code& ec); template<class ConstBufferSequence> size_t send(const ConstBufferSequence& buffers, socket_base::message_flags flags); template<class ConstBufferSequence> size_t send(const ConstBufferSequence& buffers, socket_base::message_flags flags, error_code& ec); template<class ConstBufferSequence, class CompletionToken> DEDUCED async_send(const ConstBufferSequence& buffers, CompletionToken&& token); template<class ConstBufferSequence, class CompletionToken> DEDUCED async_send(const ConstBufferSequence& buffers, socket_base::message_flags flags, CompletionToken&& token); template<class ConstBufferSequence> size_t send_to(const ConstBufferSequence& buffers, const endpoint_type& recipient); template<class ConstBufferSequence> size_t send_to(const ConstBufferSequence& buffers, const endpoint_type& recipient, error_code& ec); template<class ConstBufferSequence> size_t send_to(const ConstBufferSequence& buffers, const endpoint_type& recipient, socket_base::message_flags flags); template<class ConstBufferSequence> size_t send_to(const ConstBufferSequence& buffers, const endpoint_type& recipient, socket_base::message_flags flags, error_code& ec); template<class ConstBufferSequence, class CompletionToken> DEDUCED async_send_to(const ConstBufferSequence& buffers, const endpoint_type& recipient, CompletionToken&& token); template<class ConstBufferSequence, class CompletionToken> DEDUCED async_send_to(const ConstBufferSequence& buffers, const endpoint_type& recipient, socket_base::message_flags flags, CompletionToken&& token); }; } // inline namespace v1 } // namespace net } // namespace experimental } // namespace std
Instances of class template basic_datagram_socket meet the requirements of Destructible (C++ 2014 [destructible]), MoveConstructible (C++ 2014 [moveconstructible]), and MoveAssignable (C++ 2014 [moveassignable]).
If a program performs a synchronous operation on this socket, other than close, cancel, shutdown, send, or send_to, while there is an outstanding asynchronous read operation, the behavior is undefined.
If a program performs a synchronous operation on this socket, other than close, cancel, shutdown, receive, or receive_from, while there is an outstanding asynchronous write operation, the behavior is undefined.
When an operation has its effects specified as if by passing the result of native_handle() to a POSIX function, then the operation fails with error condition errc::bad_file_descriptor if is_open() == false at the point in the effects when the POSIX function is called.
If native_handle_type and basic_socket<Protocol>::native_handle_type are both defined then they name the same type.
explicit basic_datagram_socket(io_context& ctx);
Effects: Initializes the base class with basic_socket<Protocol>(ctx).
basic_datagram_socket(io_context& ctx, const protocol_type& protocol);
Effects: Initializes the base class with basic_socket<Protocol>(ctx, protocol).
basic_datagram_socket(io_context& ctx, const endpoint_type& endpoint);
Effects: Initializes the base class with basic_socket<Protocol>(ctx, endpoint).
basic_datagram_socket(io_context& ctx, const protocol_type& protocol,
const native_handle_type& native_socket);
Effects: Initializes the base class with basic_socket<Protocol>(ctx, protocol, native_socket).
basic_datagram_socket(basic_datagram_socket&& rhs);
Effects: Move constructs an object of class basic_datagram_socket<Protocol>, initializing the base class with basic_socket<Protocol>(std::move(rhs)).
template<class OtherProtocol>
basic_datagram_socket(basic_datagram_socket<OtherProtocol>&& rhs);
Requires: OtherProtocol is implicitly convertible to Protocol.
Effects: Move constructs an object of class basic_datagram_socket<Protocol>, initializing the base class with basic_socket<Protocol>(std::move(rhs)).
Remarks: This constructor shall not participate in overload resolution unless OtherProtocol is implicitly convertible to Protocol.
basic_datagram_socket& operator=(basic_datagram_socket&& rhs);
Effects: Equivalent to basic_socket<Protocol>::operator=(std::move(rhs)).
Returns: *this.
template<class OtherProtocol>
basic_datagram_socket& operator=(basic_datagram_socket<OtherProtocol>&& rhs);
Requires: OtherProtocol is implicitly convertible to Protocol.
Effects: Equivalent to basic_socket<Protocol>::operator=(std::move(rhs)).
Returns: *this.
Remarks: This assignment operator shall not participate in overload resolution unless OtherProtocol is implicitly convertible to Protocol.
template<class MutableBufferSequence>
size_t receive(const MutableBufferSequence& buffers);
template<class MutableBufferSequence>
size_t receive(const MutableBufferSequence& buffers,
error_code& ec);
Returns: receive(buffers, socket_base::message_flags(), ec).
template<class MutableBufferSequence>
size_t receive(const MutableBufferSequence& buffers,
socket_base::message_flags flags);
template<class MutableBufferSequence>
size_t receive(const MutableBufferSequence& buffers,
socket_base::message_flags flags, error_code& ec);
A read operation ([buffer.reqmts.read.write]).
Effects: Constructs an array iov of POSIX type struct iovec and length iovlen, corresponding to buffers, and reads data from this socket as if by POSIX:
msghdr message; message.msg_name = nullptr; message.msg_namelen = 0; message.msg_iov = iov; message.msg_iovlen = iovlen; message.msg_control = nullptr; message.msg_controllen = 0; message.msg_flags = 0; recvmsg(native_handle(), &message, static_cast<int>(flags));
Returns: On success, the number of bytes received. Otherwise 0.
[ Note: This operation can be used with connection-mode or connectionless-mode sockets, but it is normally used with connection-mode sockets because it does not permit the application to retrieve the source endpoint of received data. — end note ]
template<class MutableBufferSequence, class CompletionToken>
DEDUCED async_receive(const MutableBufferSequence& buffers,
CompletionToken&& token);
Returns: async_receive(buffers, socket_base::message_flags(), std::forward<CompletionToken>(token)).
template<class MutableBufferSequence, class CompletionToken>
DEDUCED async_receive(const MutableBufferSequence& buffers,
socket_base::message_flags flags,
CompletionToken&& token);
Completion signature: void(error_code ec, size_t n).
Effects: Initiates an asynchronous operation to read data from this socket. Constructs an array iov of POSIX type struct iovec and length iovlen, corresponding to buffers, then reads data as if by POSIX:
msghdr message; message.msg_name = nullptr; message.msg_namelen = 0; message.msg_iov = iov; message.msg_iovlen = iovlen; message.msg_control = nullptr; message.msg_controllen = 0; message.msg_flags = 0; recvmsg(native_handle(), &message, static_cast<int>(flags));
If the operation completes successfully, n is the number of bytes received. Otherwise n is 0.
[ Note: This operation can be used with connection-mode or connectionless-mode sockets, but it is normally used with connection-mode sockets because it does not permit the application to retrieve the source endpoint of received data. — end note ]
template<class MutableBufferSequence>
size_t receive_from(const MutableBufferSequence& buffers,
endpoint_type& sender);
template<class MutableBufferSequence>
size_t receive_from(const MutableBufferSequence& buffers,
endpoint_type& sender, error_code& ec);
Returns: receive_from(buffers, sender, socket_base::message_flags(), ec).
template<class MutableBufferSequence>
size_t receive_from(const MutableBufferSequence& buffers,
endpoint_type& sender,
socket_base::message_flags flags);
template<class MutableBufferSequence>
size_t receive_from(const MutableBufferSequence& buffers,
endpoint_type& sender,
socket_base::message_flags flags,
error_code& ec);
A read operation ([buffer.reqmts.read.write]).
Effects: Constructs an array iov of POSIX type struct iovec and length iovlen, corresponding to buffers, and reads data from this socket as if by POSIX:
msghdr message; message.msg_name = sender.data(); message.msg_namelen = sender.capacity(); message.msg_iov = iov; message.msg_iovlen = iovlen; message.msg_control = nullptr; message.msg_controllen = 0; message.msg_flags = 0; ssize_t result = recvmsg(native_handle(), &message, static_cast<int>(flags)); if (result >= 0) sender.resize(message.msg_namelen);
Returns: On success, the number of bytes received. Otherwise 0.
[ Note: This operation can be used with connection-mode or connectionless-mode sockets, but it is normally used with connectionless-mode sockets because it permits the application to retrieve the source endpoint of received data. — end note ]
template<class MutableBufferSequence, class CompletionToken>
DEDUCED async_receive_from(const MutableBufferSequence& buffers,
endpoint_type& sender,
CompletionToken&& token);
Returns:
async_receive_from(buffers, sender, socket_base::message_flags(), forward<CompletionToken>(token))
template<class MutableBufferSequence, class CompletionToken>
DEDUCED async_receive_from(const MutableBufferSequence& buffers,
endpoint_type& sender,
socket_base::message_flags flags,
CompletionToken&& token);
A read operation ([buffer.reqmts.read.write]).
Completion signature: void(error_code ec, size_t n).
Effects: Initiates an asynchronous operation to read data from this socket. Constructs an array iov of POSIX type struct iovec and length iovlen, corresponding to buffers, then reads data as if by POSIX:
msghdr message; message.msg_name = sender.data(); message.msg_namelen = sender.capacity(); message.msg_iov = iov; message.msg_iovlen = iovlen; message.msg_control = nullptr; message.msg_controllen = 0; message.msg_flags = 0; ssize_t result = recvmsg(native_handle(), &message, static_cast<int>(flags)); if (result >= 0) sender.resize(message.msg_namelen);
If the operation completes successfully, n is the number of bytes received. Otherwise n is 0.
[ Note: This operation can be used with connection-mode or connectionless-mode sockets, but it is normally used with connectionless-mode sockets because it permits the application to retrieve the source endpoint of received data. — end note ]
template<class ConstBufferSequence>
size_t send(const ConstBufferSequence& buffers);
template<class ConstBufferSequence>
size_t send(const ConstBufferSequence& buffers, error_code& ec);
Returns: send(buffers, socket_base::message_flags(), ec).
template<class ConstBufferSequence>
size_t send(const ConstBufferSequence& buffers,
socket_base::message_flags flags);
template<class ConstBufferSequence>
size_t send(const ConstBufferSequence& buffers,
socket_base::message_flags flags, error_code& ec);
A write operation ([buffer.reqmts.read.write]).
Effects: Constructs an array iov of POSIX type struct iovec and length iovlen, corresponding to buffers, and writes data to this socket as if by POSIX:
msghdr message; message.msg_name = nullptr; message.msg_namelen = 0; message.msg_iov = iov; message.msg_iovlen = iovlen; message.msg_control = nullptr; message.msg_controllen = 0; message.msg_flags = 0; sendmsg(native_handle(), &message, static_cast<int>(flags));
Returns: On success, the number of bytes sent. Otherwise 0.
template<class ConstBufferSequence, class CompletionToken>
DEDUCED async_send(const ConstBufferSequence& buffers, CompletionToken&& token);
Returns:
async_send(buffers, socket_base::message_flags(), forward<CompletionToken>(token))
template<class ConstBufferSequence, class CompletionToken>
DEDUCED async_send(const ConstBufferSequence& buffers,
socket_base::message_flags flags,
CompletionToken&& token);
A write operation ([buffer.reqmts.read.write]).
Completion signature: void(error_code ec, size_t n).
Effects: Initiates an asynchronous operation to write data to this socket. Constructs an array iov of POSIX type struct iovec and length iovlen, corresponding to buffers, then writes data as if by POSIX:
msghdr message; message.msg_name = nullptr; message.msg_namelen = 0; message.msg_iov = iov; message.msg_iovlen = iovlen; message.msg_control = nullptr; message.msg_controllen = 0; message.msg_flags = 0; sendmsg(native_handle(), &message, static_cast<int>(flags));
If the operation completes successfully, n is the number of bytes sent. Otherwise n is 0.
template<class ConstBufferSequence>
size_t send_to(const ConstBufferSequence& buffers,
const endpoint_type& recipient);
template<class ConstBufferSequence>
size_t send_to(const ConstBufferSequence& buffers,
const endpoint_type& recipient, error_code& ec);
Returns: send_to(buffers, recipient, socket_base::message_flags(), ec).
template<class ConstBufferSequence>
size_t send_to(const ConstBufferSequence& buffers,
const endpoint_type& recipient,
socket_base::message_flags flags);
template<class ConstBufferSequence>
size_t send_to(const ConstBufferSequence& buffers,
const endpoint_type& recipient,
socket_base::message_flags flags, error_code& ec);
A write operation ([buffer.reqmts.read.write]).
Effects: Constructs an array iov of POSIX type struct iovec and length iovlen, corresponding to buffers, and writes data to this socket as if by POSIX:
msghdr message; message.msg_name = recipient.data(); message.msg_namelen = recipient.size(); message.msg_iov = iov; message.msg_iovlen = iovlen; message.msg_control = nullptr; message.msg_controllen = 0; message.msg_flags = 0; sendmsg(native_handle(), &message, static_cast<int>(flags));
Returns: On success, the number of bytes sent. Otherwise 0.
template<class ConstBufferSequence, class CompletionToken>
DEDUCED async_send_to(const ConstBufferSequence& buffers,
const endpoint_type& recipient,
CompletionToken&& token);
Returns:
async_send_to(buffers, recipient, socket_base::message_flags(), forward<CompletionToken>(token))
template<class ConstBufferSequence, class CompletionToken>
DEDUCED async_send_to(const ConstBufferSequence& buffers,
const endpoint_type& recipient,
socket_base::message_flags flags,
CompletionToken&& token);
A write operation ([buffer.reqmts.read.write]).
Completion signature: void(error_code ec, size_t n).
Effects: Initiates an asynchronous operation to write data to this socket. Constructs an array iov of POSIX type struct iovec and length iovlen, corresponding to buffers, then writes data as if by POSIX:
msghdr message; message.msg_name = recipient.data(); message.msg_namelen = recipient.size(); message.msg_iov = iov; message.msg_iovlen = iovlen; message.msg_control = nullptr; message.msg_controllen = 0; message.msg_flags = 0; sendmsg(native_handle(), &message, static_cast<int>(flags));
If the operation completes successfully, n is the number of bytes sent. Otherwise n is 0.