cpp-udpnet
cpp-udpnet is a robust, cross-platform, header-only C++ library for UDP networking. It uses cpp-pubsub for event dispatching and cpp-asyncworker for concurrent datagram processing, providing easy-to-use, non-blocking UDP capabilities.
Key Features
- Cross-Platform: Works natively on Windows (
Winsock2) and Linux/macOS (POSIX). - DNS Resolution & IPv6: Resolve hostnames dynamically. Fully supports both IPv4 and IPv6 (
AF_INET6), including dual-stack wildcard binding. - Asynchronous & Non-Blocking: High-performance background event loop ensures the main thread is never stalled by network IO.
- Built-in Thread Pool with Peer Affinity: Automatically dispatches incoming datagrams to a background worker pool, guaranteeing that callbacks for any single peer are executed serially (in-order), while different peers are processed concurrently.
- Event-Driven: Uses the
cpp-pubsubbroker to publish events whenever peers connect or time out. - Peer Address API: Retrieve the remote peer’s IP address and port from any datagram or session.
- Dynamic Port Discovery: Bind to port
0and useGetLocalPort()to safely discover OS-assigned ephemeral ports. - DNS Caching: Internal resolution caching minimizes system calls and DNS lookups during high-volume datagram dispatching.
- Multicast Support: Join and leave IPv4/IPv6 multicast groups with a single API call.
- Broadcast Support: Enable
SO_BROADCASTfor subnet-level datagram delivery. - Peer Session Tracking: Automatic peer lifecycle tracking with configurable idle timeouts.
- Highly Configurable: Fine-tune socket options (receive/send buffer sizes, broadcast), thread pool bounds, maximum datagram sizes, and idle timeouts.
- Configuration Profiles: Apply pre-tuned transport profiles (
HighThroughput,HighLatency,LowBandwidth,ReliableLAN) or customUdpProfileinstances for fast socket optimization. - Performance Metrics: Monitor cumulative bytes/packets sent and received, active peer counts, and use built-in formatting helpers for human-readable output.
- Robust Error Handling: Synchronous setup methods use exceptions (
std::system_error), while asynchronous background errors are reported via callbacks and PubSub events.
Documentation
| Page | Description |
|---|---|
| Getting Started | Installation & integration guide |
| Basic Usage | UdpListener — receiving datagrams |
| Advanced Usage | UdpSender — sending datagrams, multicast |
| Performance Metrics | Stats tracking & throughput monitoring |
| Architecture & Examples | Threading model & example programs |