jsocketpp 1.0
A cross-platform C++20 socket library.
Loading...
Searching...
No Matches
jsocketpp::DatagramPacket Class Reference

Represents a UDP datagram packet, encapsulating both payload and addressing information. More...

#include <DatagramPacket.hpp>

Public Member Functions

 DatagramPacket (const size_t size=0)
 Construct an empty DatagramPacket with a specified buffer size.
 DatagramPacket (std::string_view data, std::string addr, const Port prt)
 Construct a DatagramPacket from a string_view and destination info.
 DatagramPacket (const char *data, const size_t len, std::string addr, const Port prt)
 Construct a DatagramPacket from a raw pointer/size and destination info.
 DatagramPacket (const DatagramPacket &)=default
 DatagramPacket (DatagramPacket &&) noexcept=default
DatagramPacketoperator= (const DatagramPacket &)=default
DatagramPacketoperator= (DatagramPacket &&) noexcept=default
void resize (const size_t newSize)
 Resize the packet's internal buffer.
size_t size () const noexcept
 Get the current size of the buffer (valid bytes for receive/send).
void clear ()
 Reset the packet (clears buffer, address, and port).

Public Attributes

std::vector< char > buffer
 Data buffer for the packet payload.
std::string address {}
 Remote address (IPv4/IPv6) for the destination/source.
Port port = 0
 Remote UDP port for the destination/source.

Detailed Description

Represents a UDP datagram packet, encapsulating both payload and addressing information.

The DatagramPacket class provides a convenient way to manage the data and addressing needed for sending or receiving UDP packets using the DatagramSocket class. Each datagram packet can hold a buffer for the payload, a remote IP address, and a port number.

Typical usage:

jsocketpp::DatagramPacket packetToSend("Hello, world!", "192.168.1.10", 12345);
socket.write(packetToSend);
jsocketpp::DatagramPacket receivedPacket(1024); // Prepare a buffer for receiving
socket.read(receivedPacket);
std::cout << "Received from: " << receivedPacket.address << ":" << receivedPacket.port << std::endl;
std::cout << "Data: " << std::string(receivedPacket.buffer.begin(), receivedPacket.buffer.end()) << std::endl;
Represents a UDP datagram packet, encapsulating both payload and addressing information.
Definition DatagramPacket.hpp:48
Note
  • For sending: set the buffer, address, and port before passing to DatagramSocket::write.
  • For receiving: use an empty DatagramPacket with a pre-sized buffer; after read, the address/port will be filled in.
See also
jsocketpp::DatagramSocket
Author
MangaD
Date
2025
Version
1.0

Constructor & Destructor Documentation

◆ DatagramPacket() [1/5]

jsocketpp::DatagramPacket::DatagramPacket ( const size_t size = 0)
inlineexplicit

Construct an empty DatagramPacket with a specified buffer size.

Parameters
sizeInitial size of the internal buffer (default: 0).

◆ DatagramPacket() [2/5]

jsocketpp::DatagramPacket::DatagramPacket ( std::string_view data,
std::string addr,
const Port prt )
inline

Construct a DatagramPacket from a string_view and destination info.

Parameters
dataData to be copied into the packet buffer.
addrDestination address.
prtDestination UDP port.

◆ DatagramPacket() [3/5]

jsocketpp::DatagramPacket::DatagramPacket ( const char * data,
const size_t len,
std::string addr,
const Port prt )
inline

Construct a DatagramPacket from a raw pointer/size and destination info.

Parameters
dataPointer to data buffer.
lenNumber of bytes to copy.
addrDestination address.
prtDestination UDP port.

◆ DatagramPacket() [4/5]

jsocketpp::DatagramPacket::DatagramPacket ( const DatagramPacket & )
default

◆ DatagramPacket() [5/5]

jsocketpp::DatagramPacket::DatagramPacket ( DatagramPacket && )
defaultnoexcept

Member Function Documentation

◆ clear()

void jsocketpp::DatagramPacket::clear ( )
inline

Reset the packet (clears buffer, address, and port).

◆ operator=() [1/2]

DatagramPacket & jsocketpp::DatagramPacket::operator= ( const DatagramPacket & )
default

◆ operator=() [2/2]

DatagramPacket & jsocketpp::DatagramPacket::operator= ( DatagramPacket && )
defaultnoexcept

◆ resize()

void jsocketpp::DatagramPacket::resize ( const size_t newSize)
inline

Resize the packet's internal buffer.

Parameters
newSizeThe new size for the buffer.

◆ size()

size_t jsocketpp::DatagramPacket::size ( ) const
inlinenodiscardnoexcept

Get the current size of the buffer (valid bytes for receive/send).

Returns
Size of buffer in bytes.

Member Data Documentation

◆ address

std::string jsocketpp::DatagramPacket::address {}

Remote address (IPv4/IPv6) for the destination/source.

  • On send: set to the destination address.
  • On receive: will be filled with sender's address.

◆ buffer

std::vector<char> jsocketpp::DatagramPacket::buffer

Data buffer for the packet payload.

  • On sending: Contains the data to transmit.
  • On receiving: Filled with received data (size indicates bytes received).

◆ port

Port jsocketpp::DatagramPacket::port = 0

Remote UDP port for the destination/source.

  • On send: set to the destination port.
  • On receive: will be filled with sender's port.

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