ASL
MulticastSocket Class Reference

Detailed Description

A communication socket for multicast UDP/IP protocol.

A socket can send packets to a multicast group and port, and another socket can join the group so it receives packets sent to it.

InetAddress group("224.0.1.1", 18000);

A listening side would bind to the port on a local interface, join the group, receive packets and then optionally leave (on socket destruction the group is automatically left):

MulticastSocket socket;
socket.bind(group.port());
socket.join(group);
InetAddress sender;
socket.readFrom(sender, data);
socket.leave(group);

And a sending side would start a multicast session to the same group and send packets to it:

MulticastSocket socket;
socket.sendTo(group, data);

#include <Socket.h>

Inheritance diagram for MulticastSocket:
PacketSocket Socket

Public Member Functions

bool join (const InetAddress &a, int interfac=0)
 Joins a multicast group. More...
 
bool leave (const InetAddress &a, int interfac=0)
 Leaves a multicast group and stops receiving packets from it.
 
bool setLoop (bool on)
 Enables receiving packets in the loopback interface.
 
bool setTTL (int n)
 Sets the multicast socket's TTL value (default 1)
 
void multicast (const InetAddress &a, int ttl=1)
 Starts a multicast session for group address a. More...
 
- Public Member Functions inherited from PacketSocket
String void sendTo (const InetAddress &addr, const void *data, int n)
 Sends n bytes from data as a packet to the address addr.
 
void sendTo (const InetAddress &addr, const ByteArray &data)
 Sends a data packet to the address addr.
 
int readFrom (InetAddress &addr, void *data, int n)
 Reads an incoming packet into the buffer data of size n, writes the address of the sending peer and returns the actual packet size.
 
ByteArray readFrom (InetAddress &addr, int n=1000)
 Reads an incoming packet of at most n bytes and returns it, and gets the address of the sending peer.
 
- Public Member Functions inherited from Socket
bool disconnected ()
 Checks if the connection was lost.
 
bool connected ()
 Checks if the connection is open.
 
bool bind (const String &ip, int port)
 Binds this socket to the given IP address and port number.
 
bool bind (int port)
 Binds this socket with the given port number to all IPv4 interfaces.
 
void listen (int n=5)
 Makes this socket listen to incoming connections.
 
Socket accept ()
 For a listening socket, waits for an incoming connection and returns a socket to communicate with the remote peer.
 
InetAddress remoteAddress () const
 Returns the adress of the remote peer for a connected socket.
 
InetAddress localAddress () const
 Returns the adress of this connected socket if bound.
 
bool connect (const String &host, int port)
 Connects this socket to the given host name and port, trying all addresses assigned to that name until one succeeds.
 
bool connect (const String &host)
 Connects this socket to the given address given as a string "host:port" or "[ipv6]:port".
 
void close ()
 Closes this socket.
 
String readLine ()
 Reads a line of text from the socket (keep in mind this is not buffered, can be a bit inefficient, and there is a length limit of 8000 bytes)
 
int available ()
 Returns the number of bytes available for reading without blocking.
 
int read (void *data, int size)
 Reads size bytes from the socket into the bufffer pointed to by data.
 
int write (const void *data, int n)
 Writes n bytes from the bufffer pointed to by data to the socket.
 
int write (const ByteArray &data)
 Writes the byte array to the socket and returns the number of bytes actually sent.
 
ByteArray read (int n=-1)
 Reads n bytes and returns them as an array of bytes, or reads all available bytes if no argument is given.
 
void skip (int n)
 Skips (reads and discards) the next n bytes from the socket.
 
bool waitInput (double timeout=2)
 Waits until there is incoming data in the socket or it is disconnected for a maximum time, and returns true if some of that happened before timeout.
 
bool waitData (double timeout=2)
 Waits until there is incoming data in the socket or it is disconnected for a maximum time, and returns true only if there is data to read (false may mean no data or disconnection)
 
int error () const
 Returns true if there was some communication error in this socket.
 
String errorMsg () const
 Returns a string representation of the last socket error.
 
template<class T >
Socketoperator<< (const T &x)
 Writes variable x to the socket respecting endianness in binary form.
 
template<class T >
Socketoperator>> (T &x)
 Reads variable x from the socket respecting endianness in binary form.
 
String readString (int n)
 Reads n bytes from the socket a string.
 
Socketoperator>> (String &x)
 Reads a string from the socket that is preceded by its length as an int32 (the sender must send the byte length before) More...
 

Member Function Documentation

◆ join()

bool join ( const InetAddress a,
int  interfac = 0 
)
inline

Joins a multicast group.

Packets sent to the group's address will be received

◆ multicast()

void multicast ( const InetAddress a,
int  ttl = 1 
)

Starts a multicast session for group address a.

Packets sent will be received by all sockets that join the group.

Deprecated:
Use .connect() or .sendTo()

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