Internet Protocol
Header Only Library

UDP Client

How to use UDP client class

References

#include "ip.hpp"
#include "ip/udp/udpclient.hpp"

Syntax

namespace InternetProtocol
class udp_server_c

Example code

#include <iostream>
#include "ip.hpp"

using namespace internetprotocol;

int main()
{
    udp_server_c net;

    std::set<udp::endpoint> endpoints{};
    net.on_error = [&](const asio::error_code &ec) {
        std::cout << ec.message() << std::endl;
    };
    net.on_message_received = [&](const std::vector<uint8_t> &buffer, const size_t bytes_recvd, const udp::endpoint &endpoint) {
        if (endpoints.find(endpoint) == endpoints.end()) {
            endpoints.insert(endpoint);
        }
        std::string message = buffer_to_string(buffer);
        for (const auto &ep: endpoints) {
            net.send_to(message, ep); // Broadcast message to other clients
        }
    };
    net.bind();

    std::string input;
    while (std::getline(std::cin, input)) {
        if (input == "quit") {
            net.close();
            break;
        }
        for (const auto &ep: endpoints) {
            net.send_to(input, ep, [](const asio::error_code &ec, size_t bytes_sent, const udp::endpoint &endpoint) {
                // Check if message has been sent
            });
        }
    }
    join_threads();

    return 0;
}

Functions

Is Open

  • Return true if socket is open.
bool is_open() const

Local Endpoint

  • Get the local endpoint of the socket. Use this function only after open connection.
udp::endpoint local_endpoint() const

Remote Endpoint

  • Get the remote endpoint of the socket. Use this function only after open connection.
udp::endpoint remote_endpoint() const

Get Error Code

  • Return a const ref of the latest error code returned by asio.
const asio::error_code &get_error_code() const

Set Recv Buffer Size

  • Set receive buffer size in bytes.
void set_recv_buffer_size(const size_t size = 16384)

Get Recv Buffer Size

  • Get receive buffer size in bytes.
size_t get_recv_buffer_size() const

Send

  • Send a string message
  • Return false if message is empty or socket is closed
  • Warning: If you’re using Windows, ensure that you are sending characters compatible with UTF-8
bool send(const std::string &message, const std::function<void(const asio::error_code &, const size_t)> &callback = nullptr)

Send Buffer

  • Send a buffer message
  • Return false if buffer is empty or socket is closed
Message
bool send_buffer(const std::vector<uint8_t> &buffer, const std::function<void(const asio::error_code &, const size_t)> &callback = nullptr)

Connect

  • Connect to the host
  • Return false if socket is already open
bool connect(const client_bind_options_t &bind_opts = {})

Close

  • Close connection
void close()

Events

On Connected

  • Event triggered when a connection is estabilished
std::function<void()> on_connected;

On Message Received

  • Event triggered when a message is received
  • Use buffer_to_string() function to converte const std::vector<uint8_t> & to std::string
std::function<void(const std::vector<uint8_t> &, const size_t, const udp::endpoint &)> on_message_received

On Close

  • Event triggered when socket is closed
std::function<void()> on_close

On Error

  • Event triggered if any error occur during async process
  • const asio::error_code &: if error occur, error code will be different from 0
  • Normally, when an error occurs, the socket is automatically closed, triggering the on_close event.
std::function<void(const asio::error_code &)> on_error