|
| MulticastSocket (Port port=0, std::size_t bufferSize=2048) |
| Constructs a multicast socket optionally bound to a local port and buffer size.
|
void | joinGroup (const std::string &groupAddr, const std::string &iface="") |
| Joins a multicast group.
|
void | leaveGroup (const std::string &groupAddr, const std::string &iface="") |
| Leaves a multicast group.
|
void | setMulticastInterface (const std::string &iface) |
| Sets the default interface for outgoing multicast packets.
|
std::string | getMulticastInterface () const |
| Gets the currently set interface for outgoing multicast packets.
|
void | setTimeToLive (int ttl) |
| Set the time-to-live (TTL) for outgoing multicast packets.
|
int | getTimeToLive () const |
| Get the current time-to-live (TTL) value for outgoing multicast packets.
|
void | setLoopbackMode (bool enable) |
| Enables or disables multicast loopback.
|
int | getLoopbackMode () const |
| Get the current multicast loopback mode setting.
|
std::string | getCurrentGroup () const |
| Returns the last multicast group joined (for reference/debug).
|
| DatagramSocket (Port port, std::size_t bufferSize=2048) |
| Constructs a UDP socket optionally bound to a local port.
|
| DatagramSocket (std::string_view host, Port port, std::size_t bufferSize=2048) |
| Construct a datagram socket to a remote host and port.
|
| ~DatagramSocket () noexcept |
| Destructor. Closes the socket and frees resources.
|
| DatagramSocket (const DatagramSocket &)=delete |
| Copy constructor (deleted).
|
DatagramSocket & | operator= (const DatagramSocket &)=delete |
| Copy assignment operator (deleted).
|
| DatagramSocket (DatagramSocket &&other) noexcept |
| Move constructor.
|
DatagramSocket & | operator= (DatagramSocket &&other) noexcept |
| Move assignment operator.
|
void | bind () const |
| Bind the datagram socket to the configured port set in the constructor.
|
void | connect (int timeoutMillis=-1) |
| Connects the datagram socket to the remote host and port with optional timeout handling.
|
size_t | write (const DatagramPacket &packet) const |
| Write data to a remote host using a DatagramPacket.
|
size_t | write (std::string_view message) const |
| Write data to the socket (connected UDP) from a string_view.
|
size_t | write (std::string_view message, std::string_view host, Port port) const |
| Send a datagram to a specific host and port (connectionless UDP).
|
size_t | read (DatagramPacket &packet, bool resizeBuffer=true) const |
| Receives a UDP datagram and fills the provided DatagramPacket.
|
template<typename T> |
T | read () |
| Read a trivially copyable type from the socket (connected UDP).
|
template<typename T> |
T | recvFrom (std::string *senderAddr, Port *senderPort) |
| Receive a datagram into a trivially copyable type.
|
void | close () |
| Close the datagram socket.
|
void | setNonBlocking (bool nonBlocking) const |
| Set the socket to non-blocking or blocking mode.
|
void | setTimeout (int millis) const |
| Sets the socket's receive timeout.
|
void | setOption (int level, int optName, int value) const |
| Set a socket option (integer value).
|
int | getOption (int level, int optName) const |
| Get a socket option (integer value).
|
std::string | getLocalSocketAddress () const |
| Get the local socket's address as a string (ip:port).
|
bool | isValid () const |
| Check if the datagram socket is valid (open).
|
bool | isConnected () const |
| Check if the datagram socket is connected to a remote host.
|
void | enableBroadcast (bool enable) const |
| Enable or disable the ability to send broadcast packets.
|
Cross-platform multicast UDP socket class (IPv4/IPv6).
The MulticastSocket class extends DatagramSocket to provide high-level, Java-inspired support for multicast networking. It works on both Windows and Linux, and supports both IPv4 and IPv6 multicast groups.
What is Multicast?
- Multicast allows you to send a single UDP packet to multiple hosts subscribed to a multicast group address.
- It is useful for applications such as real-time data feeds, streaming media, and network discovery protocols.
- Each group is identified by a special IP address (224.0.0.0–239.255.255.255 for IPv4, ff00::/8 for IPv6).
Usage Example (Receiving)
*
*
socket.joinGroup("239.255.0.1");
*
size_t n = socket.read(packet);
std::string data(packet.buffer.begin(), packet.buffer.begin() + n);
std::cout << "Received: " << data << " from " << packet.address << ":" << packet.port << std::endl;
socket.leaveGroup("239.255.0.1");
Represents a UDP datagram packet, encapsulating both payload and addressing information.
Definition DatagramPacket.hpp:48
MulticastSocket(Port port=0, std::size_t bufferSize=2048)
Constructs a multicast socket optionally bound to a local port and buffer size.
Definition MulticastSocket.cpp:5
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:13
Usage Example (Sending)
size_t write(const DatagramPacket &packet) const
Write data to a remote host using a DatagramPacket.
Definition DatagramSocket.cpp:232
void setTimeToLive(int ttl)
Set the time-to-live (TTL) for outgoing multicast packets.
Definition MulticastSocket.cpp:393
Features
- Join/leave multicast groups on a specific interface or all interfaces.
- Works with both IPv4 and IPv6 multicast groups.
- Set multicast TTL (time-to-live/hop limit).
- Set outgoing interface for multicast packets.
- Control whether multicast packets sent by this socket are received by itself (loopback).
- Modern, Java-style, exception-safe C++ API.
- See also
- jsocketpp::DatagramSocket
-
jsocketpp::DatagramPacket