When a datagram socket is defined, the setsockopt() call can be used to modify it. In order to join or leave a multicast group, use the setsockopt() call with the IP_ADD_MEMBERSHIP or IP_DROP_MEMBERSHIP flags. The interface that is used and the group used are specified in an ip_mreq structure that contains the following fields:
struct ip_mreq{ struct in_addr imr_multiaddr; struct in_addr imr_interface; }
The in_addr structure is defined as:
struct in_addr{ ulong s_addr; }
A host group consists of zero or more IP hosts. An IP datagram designated to a host group address will be delivered to all the current members of that group. A host group does not have a fixed membership. Any IP multicast-capable hosts can join or leave a host group dynamically.
To join or leave a host group, an application needs to specify a host group address, ranging from 224.0.0.2 to 239.255.255.255, and a network interface address. (Note that 224.0.0.0 is reserved and 224.0.0.1 is permanently assigned to the all hosts group, which includes all hosts and routers participating in IP multicast.) It is possible to join the same host group on more than one network interface. It is also possible for more than one application to join the same host group. A host's IP module discards an incoming multicast datagram designated for a host group not joined on that incoming network interface, even though the same host group is joined on another network interface.
In order to send to a multicasting group it is not necessary to join the groups. For receiving transmissions sent to a multicasting group membership is required. For multicast sending use an IP_MULTICAST_IF flag with the setsockopt() call. This specifies the interface to be used.
An application can specify the time-to-live value of outgoing multicast datagrams. The default value is one for all IP multicast datagrams. An application can also specify whether a copy of the multicast datagram is looped back in the case where the host itself is a member of the destination host group. By default, a copy of the multicast datagram is looped back.
It may be necessary to call the setsockopt() call with the IP_MULTICAST_LOOP flag in order to control the loopback of multicast packets. By default, packets are delivered to all members of the multicast group including the sender, if it is a member. However, this can be disabled with the setsockopt() call using the IP_MULTICAST_LOOP flag.
For hosts that attach to more than one network, an application can choose which interface is used for the initial transmission. Only one interface can be used for the initial transmission. Further transmission to other networks is the responsibility of multicast routers. Do not be confused by the interface where a host group joins the outgoing interface for multicast transmission. The interface specified when joining or leaving a host group is mainly for receiving incoming multicast datagrams. An application can join one host group on one network interface but transmit data to the same host group by way of another interface.
Currently multicast is supported only on UDP, so datagram sockets should be used to do multicast operations. One thing to consider is that using aliasing and multicasting together (with multiple processes) may give unexpected results, and the following limitations apply.
The setsockopt() call flags that are required for multicast communication and used with the IPPROTO_IP protocol level follow:
IP_ADD_MEMBERSHIP
The getsockopt() function can also be used with the multicast flags to obtain information about a particular socket:
IP_MULTICAST_IF
The following examples demonstrate the use of the setsockopt() call with the protocol level set to the Internet Protocol (IPPROTO_IP).
To mark a socket for sending to a multicast group on a particular interface:
struct ip_mreq imr;setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &imr.imr_interface, sizeof(struct in_addr));
To disable the loopback on a socket:
char loop = 0; setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(uchar));
To allow address reuse for binding multiple multicast applications to the same IP group address:
int on = 1; setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int));
To join a multicast group for receiving:
struct ip_mreq imr; setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr, sizeof(struct ip_mreq));
To leave a multicast group:
struct ip_mreq imr; setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &imr, sizeof(struct ip_mreq));