Internet Protocol
Header Only Library

UDP Server

How to use UDP server class

References

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

Syntax

namespace internetprotocol
class udp_server_c

Example code

#include <iostream>
#include <ip/udp/udpserver.hpp>

using namespace internetprotocol;

int main(int argc, char** argv) {
    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

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 To

  • Broadcasts a string to an specific endpoint.
  • 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_to(const std::string &message, const udp::endpoint &endpoint, const std::function<void(const asio::error_code &, const size_t, const udp::endpoint &)> &callback = nullptr)

Send Buffer To

  • Broadcasts a buffer to an specific endpoint.
  • Return false if buffer is empty or socket is closed
bool send_buffer_to(const std::vector<uint8_t> &buffer, const udp::endpoint &endpoint, const std::function<void(const asio::error_code &, const size_t)> &callback = nullptr)

Bind

  • Listen for datagram messages on a named port and optional address.
  • Return false if socket is already open or any error occurs
  • Event on_error is triggered if any error occurs
bool bind(const server_bind_options_t &bind_opts = { "", 8080, v4, true })

Close

  • Close the underlying socket and stop listening for data on it. 'on_close' event will be triggered.
void close()

Events

On Listening

  • Event triggered when socket start to listening
std::function<void()> on_listening

On Message Received

  • Event triggered when a message is received by a endpoint
  • Use buffer_to_string() function to converte 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