Examples: Use multicasting with AF_INET

IP multicasting provides the capability for an application to send a single IP datagram that a group of hosts in a network can receive.

The hosts that are in the group might reside on a single subnet or on different subnets that connect multicast-capable routers. Hosts might join and leave groups at any time. There are no restrictions on the location or number of members in a host group. A class D IP address in the range 224.0.0.1 to 239.255.255.255 identifies a host group.

An application program can send or receive multicast datagrams by using the socket() API and connectionless SOCK_DGRAM type sockets. Multicasting is a one-to-many transmission method. You cannot use connection-oriented sockets of type SOCK_STREAM for multicasting. When a socket of type SOCK_DGRAM is created, an application can use the setsockopt() function to control the multicast characteristics associated with that socket. The setsockopt() function accepts the following IPPROTO_IP level flags:

Note: i5/OS™ sockets support IP multicasting for the AF_INET address family.


This graphic shows the flow of socket calls that are used for IP multicasting with AF_INET address family.

Socket flow of events: Sending multicast datagrams

The following sequence of the socket calls provides a description of the graphic. It also describes the relationship between two applications that send and receive multicast datagrams. The first example uses the following sequence of function calls:

  1. The socket() function returns a socket descriptor representing an endpoint. The statement also identifies that the INET (Internet Protocol) address family with the TCP transport (SOCK_DGRAM) is used for this socket. This socket sends datagrams to another application.
  2. The sockaddr_in structure specifies the destination IP address and port number. In this example, the address is 225.1.1.1 and the port number is 5555.
  3. The setsockopt() function sets the IP_MULTICAST_LOOP socket option so that the sending system does not receive a copy of the multicast datagrams it transmits.
  4. The setsockopt() function uses the IP_MULTICAST_IF socket option which defines the local interface over which the multicast datagrams are sent.
  5. The sendto() function sends multicast datagrams to the specified group IP addresses.
  6. The close() function closes any open socket descriptors.

Socket flow of events: Receive multicast datagrams

The second example uses the following sequence of function calls:

  1. The socket() function returns a socket descriptor representing an endpoint. The statement also identifies that the INET (Internet Protocol) address family with the TCP transport (SOCK_DGRAM) is used for this socket. This socket sends datagrams to another application.
  2. The setsockopt() function sets the SO_REUSEADDR socket option to allow multiple applications to receive datagrams that are destined to the same local port number.
  3. The bind() function specifies the local port number. In this example, the IP address is specified as INADDR_ANY to receive datagrams that are addressed to the multicast group.
  4. The setsockopt() function uses the IP_ADD_MEMBERSHIP socket option which joins the multicast group that receives the datagrams. When joining a group, specify the class D group address along with the IP address of a local interface. The system must call the IP_ADD_MEMBERSHIP socket option for each local interface receiving the multicast datagrams. In this example, the multicast group (225.1.1.1) is joined on the local 9.5.1.1 interface.
    Note: IP_ADD_MEMBERSHIP option must be called for each local interface over which the multicast datagrams are to be received.
  5. The read() function reads multicast datagrams that are being sent..
  6. The close() function closes any open socket descriptors.
Related concepts
IP multicasting
Related reference
Example: Send multicast datagrams
Related information
close()
socket()
bind()
setsockopt
read()
sendto()