Class SocketUDP

Class Documentation

class SocketUDP

Manage a UDP connection with a server, receiving and sending messages.

Public Functions

SocketUDP(const std::string &server_address, uint16_t client_port, uint16_t server_port)

Constructor to create a UDP connection with a server.

Parameters:
  • server_address – The server address.

  • server_port – The server port.

  • command_address – Default address for sending commands.

  • command_port – Default port for sending commands.

~SocketUDP()

Destructor to close the socket file descriptor.

void closeSocket()

Close the socket file descriptor.

bool send(const std::string &message, std::string &response, int timeout_milis = 0)

Send a message to the server and optionally wait for a response.

Parameters:
  • message – The message to send.

  • response – The response received reference.

  • timeout_milis – Timeout in milliseconds. If zero, dont wait for a response. If negative, wait indefinitely. Default: 0

Returns:

True if the message was sent successfully, false otherwise. If waiting for a response, true if a response was received, false otherwise.

bool receive(std::string &response, const int timeout_milis = 0)

Receive a message from the server.

Parameters:
  • response – The response received reference.

  • timeout_milis – Timeout in milliseconds to wait for a response. If zero, dont wait for a response. If negative, wait indefinitely.

Returns:

True if a response was received, false otherwise.