libcluon  0.0.148
cluon::UDPSender Class Reference

#include <UDPSender.hpp>

Public Member Functions

 UDPSender (const std::string &sendToAddress, uint16_t sendToPort) noexcept
 
 ~UDPSender () noexcept
 
std::pair< ssize_t, int32_t > send (std::string &&data) const noexcept
 
uint16_t getSendFromPort () const noexcept
 

Detailed Description

To send data using a UDP socket, simply include the header #include <cluon/UDPSender.hpp>.

Next, create an instance of class cluon::UDPSender as follows: cluon::UDPSender sender("127.0.0.1", 1234);. The first parameter is of type std::string expecting a numerical IPv4 address and the second parameter specifies the UDP port to which the data shall be sent to.

To finally send data, simply call the method send supplying the data to be sent: sender.send(std::move("Hello World!"). Please note that the data is supplied using the move-semantics. The method send returns a std::pair<ssize_t, int32_t> where the first element returns the size of the successfully sent bytes and the second element contains the error code in case the transmission of the data failed.

cluon::UDPSender sender("127.0.0.1", 1234);
std::pair<ssize_t, int32_t> retVal = sender.send(std::move("Hello World!"));
std::cout << "Send " << retVal.first << " bytes, error code = " << retVal.second << std::endl;

A complete example is available here.

Constructor & Destructor Documentation

◆ UDPSender()

cluon::UDPSender::UDPSender ( const std::string &  sendToAddress,
uint16_t  sendToPort 
)
noexcept

Constructor.

Parameters
sendToAddressNumerical IPv4 address to send a UDP packet to.
sendToPortPort to send a UDP packet to.

References cluon::getIPv4FromHostname(), and retVal.

◆ ~UDPSender()

cluon::UDPSender::~UDPSender ( )
noexcept

Member Function Documentation

◆ getSendFromPort()

uint16_t cluon::UDPSender::getSendFromPort ( ) const
noexcept
Returns
Port that this UDP sender will use for sending or 0 if no information available.

◆ send()

std::pair< ssize_t, int32_t > cluon::UDPSender::send ( std::string &&  data) const
noexcept

Send a given string.

Parameters
dataData to send.
Returns
Pair: Number of bytes sent and errno.

References cluon::MAX_SIZE_UDP_PACKET, cluon::SIZE_IPv4_HEADER, and cluon::SIZE_UDP_HEADER.