The Micro C++ Library
|
A MulticastSocket represents an Internet Protocol (IP) specific Multicast DatagramSocket that adds capabilities for joining multicast groups of other multicast hosts on the internet. More...
#include <micxx.hxx>
Inherits micxx::DatagramSocket.
Public Member Functions | |
MulticastSocket () | |
Create a new MulticastSocket. | |
virtual unsigned char | getMulticastTTL () const |
The current value for socket option IP_MULTICAST_TTL of level IPPROTO_IP . | |
virtual void | join (const MulticastAddress &group) |
Join the specified multicast group. | |
virtual void | join (const InetAddress &group, const InetAddress *intfAddress=0) |
Join the specified multicast group at the specified interface intfAddress. | |
virtual void | join (const std::string &group) |
Join the specified multicast group. | |
virtual void | leave (const MulticastAddress &group) |
Leave the specified multicast group. | |
virtual void | leave (const InetAddress &group, const InetAddress *intfAddress=0) |
Leave the specified multicast group at the specified interface intfAddress. | |
virtual void | leave (const std::string &group) |
Leave the specified multicast group. | |
virtual void | setMulticastTTL (unsigned char value) |
Set the current value for socket option IP_MULTICAST_TTL of level IPPROTO_IP . | |
Protected Member Functions | |
virtual void | createSocket (int addressFamily) |
Create the underlying socket descriptor for the specified addressFamily. |
A MulticastSocket represents an Internet Protocol (IP) specific Multicast DatagramSocket that adds capabilities for joining multicast groups of other multicast hosts on the internet.
A "multicast group" is specified by an IP address and by a standard UDP port number.
Example:
#include <micxx.hxx> using namespace micxx; using namespace std; void receiver(const InetAddress & group) { MulticastSocket socket; socket.setReuseAddress(true); socket.bind(group.getPort()); socket.join(group); InetDatagram datagram; datagram.setCapacity(socket.getReceiveBufferSize()); for (int i = 1; i < 100; ++i) { socket.recv(datagram); cout << "RECEIVED FROM " << *datagram.getSrcAddress() << " " << (const char*)datagram.getAddress() << endl; } } void sender(const InetAddress & group) { MulticastSocket socket; socket.setMulticastTTL(255); socket.bind(); while (true) { socket.sendto("Hello World!", 13, group); } } int main(int argc, const char *args[]) { System system(argc, args); try { wstring mode = System::getProperties()[L"mode"]; InetAddress group("228.1.1.2", 9001); if (mode.compare(L"send") == 0) { sender(group); } else { receiver(group); } } catch (const Exception & exception) { cerr << exception << endl; return EXIT_FAILURE; } return EXIT_SUCCESS; }
micxx::MulticastSocket::MulticastSocket | ( | ) |
Create a new MulticastSocket.
virtual void micxx::MulticastSocket::createSocket | ( | int | addressFamily | ) | [protected, virtual] |
Create the underlying socket descriptor for the specified addressFamily.
addressFamily | The family of the stored SocketAddress. |
Reimplemented from micxx::DatagramSocket.
virtual unsigned char micxx::MulticastSocket::getMulticastTTL | ( | ) | const [virtual] |
The current value for socket option IP_MULTICAST_TTL
of level IPPROTO_IP
.
virtual void micxx::MulticastSocket::join | ( | const std::string & | group | ) | [virtual] |
Join the specified multicast group.
group | may be a multicast address notation like m.m.m.m[:xxxxx][@n.n.n.n[:yyyyy]]. |
micxx::MulticastAddress groupAddress(group); join(groupAddress);
virtual void micxx::MulticastSocket::join | ( | const MulticastAddress & | group | ) | [virtual] |
Join the specified multicast group.
group | The multicast group address to be used for joining. |
join(group, group.getIntfAddress())
virtual void micxx::MulticastSocket::join | ( | const InetAddress & | group, |
const InetAddress * | intfAddress = 0 |
||
) | [virtual] |
Join the specified multicast group at the specified interface intfAddress.
Joining a multicast group is implemented by setting a struct ip_mreq
value for socket option IP_ADD_MEMBERSHIP
for level IPPROTO_IP
.
group | The group IP address to be joined. Only group.getHost() is used from group for joining. This is passed to the imr_multiaddr member of struct ip_mreq . |
intfAddress | The interface address to be used for joining, or 0 . If not 0 , only intfAddress->getHost() will be used from intfAddress for joining. This is passed to the imr_interface member of struct ip_mreq . |
virtual void micxx::MulticastSocket::leave | ( | const MulticastAddress & | group | ) | [virtual] |
Leave the specified multicast group.
group | The multicast group address to be used for leaving. |
leave(group, group.getIntfAddress())
virtual void micxx::MulticastSocket::leave | ( | const std::string & | group | ) | [virtual] |
Leave the specified multicast group.
group | may be a multicast address notation like m.m.m.m[:xxxxx][@n.n.n.n[:yyyyy]]. |
micxx::MulticastAddress groupAddress(group); leave(groupAddress);
virtual void micxx::MulticastSocket::leave | ( | const InetAddress & | group, |
const InetAddress * | intfAddress = 0 |
||
) | [virtual] |
Leave the specified multicast group at the specified interface intfAddress.
Leaving a multicast group is implemented by setting a struct ip_mreq
value for socket option IP_DROP_MEMBERSHIP
for level IPPROTO_IP
.
group | The group IP address to be joined. Only group.getHost() is used from group for joining. This is passed to the imr_multiaddr member of struct ip_mreq . |
intfAddress | The interface address to be used for leaving, or 0 . If not 0 , only intfAddress->getHost() will be used from intfAddress for leaving. This is passed to the imr_interface member of struct ip_mreq . |
virtual void micxx::MulticastSocket::setMulticastTTL | ( | unsigned char | value | ) | [virtual] |
Set the current value for socket option IP_MULTICAST_TTL
of level IPPROTO_IP
.