The Micro C++ Library
micxx::MulticastSocket Class Reference

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.

List of all members.

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.

Detailed Description

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.

Note:
Before joining a multicast group, the socket must be bound successfully.
See also:
MulticastAddress,
Socket::bind(const SocketAddress &)

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;
 }
Note:
Although MulticastSocket is derived from Mutex, itself does no locking at all. If you intend to use MulticastSocket concurrently from different threads, you could use the inherited Mutex to synchronize access to the instance (depending on the scenario in which it is used).
Author:
Norbert Klose
Date:
February, 2007

Constructor & Destructor Documentation

micxx::MulticastSocket::MulticastSocket ( )

Create a new MulticastSocket.


Member Function Documentation

virtual void micxx::MulticastSocket::createSocket ( int  addressFamily) [protected, virtual]

Create the underlying socket descriptor for the specified addressFamily.

Parameters:
addressFamilyThe family of the stored SocketAddress.
See also:
SocketAddress::getFamily()

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.

See also:
getsockopt(socket_t, IPPROTO_IP, IP_MULTICAST_TTL, void*, socklen_t*)
virtual void micxx::MulticastSocket::join ( const std::string &  group) [virtual]

Join the specified multicast group.

Parameters:
groupmay be a multicast address notation like m.m.m.m[:xxxxx][@n.n.n.n[:yyyyy]].
Note:
This is semantically equivalent to
 micxx::MulticastAddress groupAddress(group);
 join(groupAddress);
virtual void micxx::MulticastSocket::join ( const MulticastAddress group) [virtual]

Join the specified multicast group.

Parameters:
groupThe multicast group address to be used for joining.
Note:
This is semantically equivalent to
 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.

Parameters:
groupThe 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.
intfAddressThe 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.
See also:
setsockopt(socket_t, IPPROTO_IP, IP_ADD_MEMBERSHIP, const struct ip_mreq*, socklen_t)
virtual void micxx::MulticastSocket::leave ( const MulticastAddress group) [virtual]

Leave the specified multicast group.

Parameters:
groupThe multicast group address to be used for leaving.
Note:
This is semantically equivalent to
 leave(group, group.getIntfAddress())
virtual void micxx::MulticastSocket::leave ( const std::string &  group) [virtual]

Leave the specified multicast group.

Parameters:
groupmay be a multicast address notation like m.m.m.m[:xxxxx][@n.n.n.n[:yyyyy]].
Note:
This is semantically equivalent to
 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.

Parameters:
groupThe 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.
intfAddressThe 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.
See also:
setsockopt(socket_t, IPPROTO_IP, IP_DROP_MEMBERSHIP, const struct ip_mreq*, socklen_t)
virtual void micxx::MulticastSocket::setMulticastTTL ( unsigned char  value) [virtual]

Set the current value for socket option IP_MULTICAST_TTL of level IPPROTO_IP.

See also:
setsockopt(socket_t, IPPROTO_IP, IP_MULTICAST_TTL, const void*, socklen_t)

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