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.
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.