18 Sockets [socket]

18.7 Class template basic_datagram_socket [socket.dgram]

18.7.3 basic_datagram_socket operations [socket.dgram.op]

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 ]

Error conditions:

  • errc::invalid_argument — if socket_base::message_peek is set in flags.

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 ]

Error conditions:

  • errc::invalid_argument — if socket_base::message_peek is set in flags.

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.