![]() |
jsocketpp 1.0
A cross-platform C++20 socket library.
|
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 | |
| DatagramPacket & | operator= (const DatagramPacket &)=default |
| DatagramPacket & | operator= (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). | |
| bool | hasDestination () const noexcept |
| Report whether this packet specifies an explicit destination (address + 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. | |
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:
|
inlineexplicit |
Construct an empty DatagramPacket with a specified buffer size.
| size | Initial size of the internal buffer (default: 0). |
|
inline |
Construct a DatagramPacket from a string_view and destination info.
| data | Data to be copied into the packet buffer. |
| addr | Destination address. |
| prt | Destination UDP port. |
|
inline |
Construct a DatagramPacket from a raw pointer/size and destination info.
| data | Pointer to data buffer. |
| len | Number of bytes to copy. |
| addr | Destination address. |
| prt | Destination UDP port. |
|
default |
|
defaultnoexcept |
|
inline |
Reset the packet (clears buffer, address, and port).
|
default |
|
defaultnoexcept |
|
inline |
Resize the packet's internal buffer.
| newSize | The new size for the buffer. |
|
inlinenodiscardnoexcept |
Get the current size of the buffer (valid bytes for receive/send).
| std::string jsocketpp::DatagramPacket::address {} |
Remote address (IPv4/IPv6) for the destination/source.
| std::vector<char> jsocketpp::DatagramPacket::buffer |
Data buffer for the packet payload.
| Port jsocketpp::DatagramPacket::port = 0 |
Remote UDP port for the destination/source.