TCP/IP Programming Manual
Before a host can receive IP multicast datagrams destined for a particular multicast group, an
application must direct the host to become a member of that multicast group. This section describes
how an application can direct a host to add itself to and remove itself from a multicast group.
An application can direct the host it is running on to join a multicast group by using the
IP_ADD_MEMBERSHIP option to the setsockopt library call as follows:
struct ip_mreq mreq;
if (setsockopt( sock, IPPROTO_IP, IP_ADD_MULTICAST, &mreq
sizeof(mreq
)) == -1)
perror("setsockopt");
The mreq variable has the following structure:
structip_mreq{
struct in_addr imr_multiaddr; /* IP multicast
address of group */
struct in_addr imr_interface; /* local IP
address of interface */
};
Each multicast group membership is associated with a particular interface. The same group can
be joined on multiple interfaces. The imr_interface variable can be specified as INADDR_ANY,
which allows an application to choose the default multicast interface. Alternatively, specifying one
of the host's local addresses allows an application to select a particular, multicast-capable interface.
The maximum number of memberships that can be added on a single socket is subject to the
IP_MAX_MEMBERSHIPS value, which is defined in the <in.h> header file.
To drop membership in a particular multicast group, use the IP_DROP_MEMBERSHIP option to
the setsockopt library call:
struct ip_mreq mreq;
if (setsockopt( sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq
sizeof(mreq
))== -1)
perror("setsockopt");
The mreq variable contains the same structure values as those values used for adding membership.
If multiple sockets request that a host join a particular multicast group, the host remains a member
of that multicast group until the last of those sockets is closed or memberships are dropped from
all the sockets.
To receive multicast datagrams sent to a specific UDP port, the receiving socket must have bound
to that port using the bind library call. More than one process can receive UDP datagrams destined
for the same port if the bind library call (described in Chapter 4) is preceded by a setsockopt
library call that specifies the SO_REUSEPORT option. The following example illustrates how to use
the SO_REUSEPORT option to the setsockopt library call:
int setreuse = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &setreuse,
sizeof(setreuse)) == -1)
perror("setsockopt");
When the SO_REUSEPORT option is set, every incoming multicast or broadcast UDP datagram
destined for the shared port is delivered to all sockets bound to that port.
Delivery of IP multicast datagrams to SOCK_RAW sockets is determined by the protocol type of the
destination.
Datagram Protocols and Flow Control
When using datagram protocols, the programmer must manage flow control. Lack of flow control
results in the receiver failing to keep up with the sender’s rate of transmission, causing a possible
overrun condition.
46 Introduction to Programming to the Guardian Sockets Library