Examples: Using multicasting with AF_INET

With IP multicasting, an application can send a single IP datagram that a group of hosts in a network can receive.

Note: By using the examples, you agree to the terms of the Code license and disclaimer information.

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() API to control the multicast characteristics associated with that socket. The setsockopt() API accepts the following IPPROTO_IP level flags:

  • IP_ADD_MEMBERSHIP: Joins the multicast group specified.
  • IP_DROP_MEMBERSHIP: Leaves the multicast group specified.
  • IP_MULTICAST_IF: Sets the interface over which outgoing multicast datagrams are sent.
  • IP_MULTICAST_TTL: Sets the Time To Live (TTL) in the IP header for outgoing multicast datagrams.
  • IP_MULTICAST_LOOP: Specifies whether a copy of an outgoing multicast datagram is delivered to the sending host as long as it is a member of the multicast group.
Note: IBM® i 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 API calls:

  1. The socket() API 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() API 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() API uses the IP_MULTICAST_IF socket option, which defines the local interface over which the multicast datagrams are sent.
  5. The sendto() API sends multicast datagrams to the specified group IP addresses.
  6. The close() API closes any open socket descriptors.

Socket flow of events: Receiving multicast datagrams

The second example uses the following sequence of API calls:

  1. The socket() API 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() API 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() API 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() API 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 that receives the multicast datagrams. In this example, the multicast group (225.1.1.1) is joined on the local 9.5.1.1 interface.
    Note: The IP_ADD_MEMBERSHIP option must be called for each local interface over which the multicast datagrams are to be received.
  5. The read() API reads multicast datagrams that are being sent.
  6. The close() API closes any open socket descriptors.