Common
common packages for kyubic
 
Loading...
Searching...
No Matches
common::UdpSocket Class Reference

UDP (Sender/Listener) Class. More...

#include <udp.hpp>

Public Member Functions

 UdpSocket ()
 Creates a socket and configures basic options.
 
 ~UdpSocket ()
 Closes the socket.
 
void setTimeout (int sec)
 Sets the send/receive timeout.
 
void setBroadcast (bool enable)
 Enables or disables broadcast.
 
bool bind (int port)
 Binds to a specified port and prepares for receiving data.
 
std::vector< uint8_t > receive (size_t max_len=1024)
 Receives data.
 
bool setDestination (const std::string &host, int port)
 Sets a fixed destination address (uses connect). This enables the use of the send() method.
 
ssize_t send (const std::vector< uint8_t > &data)
 Sends data to the fixed destination.
 
ssize_t sendTo (const std::vector< uint8_t > &data, const std::string &host, int port)
 Sends data to a specified destination (uses sendto). Can be used without calling setDestination().
 

Detailed Description

UDP (Sender/Listener) Class.

Constructor & Destructor Documentation

◆ UdpSocket()

common::UdpSocket::UdpSocket ( )

Creates a socket and configures basic options.

◆ ~UdpSocket()

common::UdpSocket::~UdpSocket ( )

Closes the socket.

Member Function Documentation

◆ bind()

bool common::UdpSocket::bind ( int  port)

Binds to a specified port and prepares for receiving data.

Parameters
portPort number to listen on.
Returns
true on success, false on failure.

◆ receive()

std::vector< uint8_t > common::UdpSocket::receive ( size_t  max_len = 1024)

Receives data.

Parameters
max_lenMaximum buffer size for reception.
Returns
std::vector<uint8_t> Received data (returns an empty vector on timeout or error).

◆ send()

ssize_t common::UdpSocket::send ( const std::vector< uint8_t > &  data)

Sends data to the fixed destination.

Parameters
dataData to be sent.
Returns
ssize_t Number of bytes sent.

◆ sendTo()

ssize_t common::UdpSocket::sendTo ( const std::vector< uint8_t > &  data,
const std::string &  host,
int  port 
)

Sends data to a specified destination (uses sendto). Can be used without calling setDestination().

Parameters
dataData to be sent.
hostDestination IP address.
portDestination port number.
Returns
ssize_t Number of bytes sent.

◆ setBroadcast()

void common::UdpSocket::setBroadcast ( bool  enable)

Enables or disables broadcast.

Parameters
enableSet to true to enable broadcast.

◆ setDestination()

bool common::UdpSocket::setDestination ( const std::string &  host,
int  port 
)

Sets a fixed destination address (uses connect). This enables the use of the send() method.

Parameters
hostDestination IP address.
portDestination port number.
Returns
true on success.

◆ setTimeout()

void common::UdpSocket::setTimeout ( int  sec)

Sets the send/receive timeout.

Parameters
secTimeout duration in seconds.

The documentation for this class was generated from the following files: