AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::network::UdpSocket Class Reference

UDP socket. More...

#include <udpSocket.hpp>

Collaboration diagram for anch::network::UdpSocket:

Public Member Functions

 UdpSocket (const std::string &host="", uint16_t port=0)
void sendTo (std::string_view data, const anch::network::Address &to)
void sendTo (std::string_view data, const std::string &host, uint16_t port)
anch::network::Address receiveFrom (std::string &data, std::size_t maxSize=MAX_DATAGRAM_SIZE)
anch::network::Address getAddress () const
anch::network::Socket & getSocket () noexcept

Static Public Attributes

static constexpr std::size_t MAX_DATAGRAM_SIZE = 65507

Detailed Description

UDP socket.

Datagrams keep their boundaries and can contain any byte ('\0' included). A datagram which does not fit in the reception buffer raises IOException (EMSGSIZE ) instead of being silently truncated.

Author
Vincent Lachenal

Constructor & Destructor Documentation

◆ UdpSocket()

anch::network::UdpSocket::UdpSocket ( const std::string & host = "",
uint16_t port = 0 )
explicit

UdpSocket constructor: bind to local address

Parameters
hostthe local host name or numeric address (empty for any address)
portthe local port (0 for an ephemeral port: see getAddress )
Exceptions
anch::network::IOExceptionbind error

Member Function Documentation

◆ getAddress()

anch::network::Address anch::network::UdpSocket::getAddress ( ) const

Local address getter (useful for ephemeral port)

Returns
the address
Exceptions
anch::network::IOExceptionerror on getsockname

◆ getSocket()

anch::network::Socket & anch::network::UdpSocket::getSocket ( )
noexcept

Socket getter (for options, timeouts, ...)

Returns
the socket

◆ receiveFrom()

anch::network::Address anch::network::UdpSocket::receiveFrom ( std::string & data,
std::size_t maxSize = MAX_DATAGRAM_SIZE )

Receive a datagram

Parameters
datareceives the datagram
maxSizethe maximum datagram size
Returns
the sender address
Exceptions
anch::network::IOExceptionreception error, or datagram larger than maxSize (EMSGSIZE )

◆ sendTo() [1/2]

void anch::network::UdpSocket::sendTo ( std::string_view data,
const anch::network::Address & to )

Send a datagram

Parameters
datathe datagram
tothe destination
Exceptions
anch::network::IOExceptionsend error (or datagram not entirely sent)

◆ sendTo() [2/2]

void anch::network::UdpSocket::sendTo ( std::string_view data,
const std::string & host,
uint16_t port )

Send a datagram (the destination is resolved for the socket address family)

Parameters
datathe datagram
hostthe destination host name or numeric address
portthe destination port
Exceptions
anch::network::IOExceptionresolution or send error

Member Data Documentation

◆ MAX_DATAGRAM_SIZE

std::size_t anch::network::UdpSocket::MAX_DATAGRAM_SIZE = 65507
staticconstexpr

Maximum UDP payload (IPv4)


The documentation for this class was generated from the following file: