Joining and Leaving a Multicast Group

Before a host can receive IP multicast datagrams, it must become a member of one or more IP multicast groups. The following code example shows how a process requests to join a multicast group by using the setsockopt function.

struct ip_mreq mreq;
// mreq is the ip_mreqstructure
{
  struct in_addr imr_multiaddr;  //The multicast group to join 
  struct in_addr imr_interface;  //The interface to join on
}

#define RECV_IP_ADDR  "225.6.7.8" // An arbitrary multicast address

mreq.imr_multiaddr.s_addr = inet_addr(RECV_IP_ADDR);
mreq.imr_interface.s_addr = INADDR_ANY;
err = setsockopt(
                    sock, 
                    IPPROTO_IP,
                    IP_ADD_MEMBERSHIP,
                    (char*)&mreq, 
                    sizeof(mreq));

A socket must bind to an address before calling setsockopt.

Every multicast group membership is associated with a single interface. It is possible to join the same group on more than one interface. To choose the default multicast interface, use INADDR_ANY as the address for imr_interface. To choose a specific interface capable of multicasting, use one of the host's local addresses.

The following code example shows how to leave a multicast group.

struct ip_mreq mreq;
setsockopt(    
            sock, 
            IPPROTO_IP, 
            IP_DROP_MEMBERSHIP,
            (char*)&mreq,
            sizeof(mreq));

The memberships associated with a socket are dropped when the socket is closed, or the process holding the socket is terminated. However, more than one socket may claim a membership in a particular group, and the host remains a member of that group until the last membership is dropped.