Class Client

Class Documentation

class Client

A RAII wrapper around network sockets providing a simple interface for sending and receiving data.

Public Functions

Client(const address_t &address, const socket_protocol protocol = socket_protocol::TCP, const address_t &bind_service = {}, const socket_config_handlers_t &config_handlers = {})

Initialise a client socket with the given address, protocol, and service/port.

Parameters:
const address_t &address

The address to connect to

const socket_protocol protocol = socket_protocol::TCP

The protocol to use (TCP/UDP)

const address_t &bind_service = {}

The service/port to bind to

const socket_config_handlers_t &config_handlers = {}

Configuration handlers for various stages of socket creation

inline Client(const address_t &address, const socket_protocol protocol, const uint16_t bind_port, const socket_config_handlers_t &config_handlers = {})
Client(const Client &other) = delete
inline Client(Client &&other) noexcept
Client &operator=(Client other) = delete
inline Client &operator=(Client &&other) noexcept
ssize_t transmit(const std::string &message)

Transmit a string over the socket.

Parameters:
const std::string &message

Data to transmit

Returns:

ssize_t Number of bytes actually transmitted

ssize_t transmit(const std::vector<uint8_t> &buffer)

Transmit a buffer over the socket.

Parameters:
const std::vector<uint8_t> &buffer

Data to transmit

Returns:

ssize_t Number of bytes actually transmitted

std::optional<std::vector<uint8_t>> receive(size_t len, bool blocking = false)

Receive data from the socket.

Note

When blocking is false, this function will return std::nullopt if there isn’t any data available

Parameters:
size_t len

Number of bytes to attempt to receive

bool blocking = false

Whether or not to block until data is available

Returns:

std::optional<std::vector<uint8_t>> Received data, if any

Friends

inline friend void swap(Client &first, Client &second) noexcept