next up previous contents
Next: Internet Up: Networking Previous: Writing a datagram client   Contents

JAVA multicast sockets

The TCP/IP protocols support broadcasting, but the use of broadcasts is limited to the local network or subnet. Multicast sockets are between broadcasting and point-to-point communication. Multicasting sends data from one host to many different hosts but not to everyone. The data goes to clients that have expressed an interest in data by joining a particular multicast group. When using multicasting you have to limit how far the packets can travel. This is possible by using another field in the header the TTL (Time-To-Live) value. The TTL in IPv4 multicasting has also the meaning of threshold. Its use becomes evident with an example: suppose you set a long, bandwidth consuming, video conference between all the hosts belonging to your department. You want that huge amount of traffic to remain in your LAN. Perhaps your department is big enough to have various LANs. In that case you want those hosts belonging to each of your LANs to attend the conference, but in any case you want to collapse the entire Internet with your multicast traffic. There is a need to limit how long multicast traffic will expand across routers. That's what the TTL is used for. Routers have a TTL threshold assigned to each of its interfaces, and only datagrams with a TTL greater than the interface's threshold are forwarded. Note that when a datagram traverses a router with a certain threshold assigned, the datagram's TTL is not decremented by the value of the threshold. Only a comparison is made. (As before, the TTL is decremented by 1 each time a datagram passes across a router). A list of TTL thresholds and their associated scope follows:

TTL Scope
0 Restricted to the same host. Won't be output by any interface.
1 Restricted to the same subnet. Won't be forwarded by a router.
$<32$ Restricted to the same site, organization or department.
$<64$ Restricted to the same region.
$<128$ Restricted to the same continent.
$<255$ Unrestricted in scope. Global.
   

Mapping of IP Multicast Addresses to Ethernet/FDDI addresses
Both Ethernet and FDDI frames have a 48 bit destination address field. In order to avoid the need for a kind of multicast ARP to map multicast IP addresses to ethernet/FDDI ones, the IANA reserved a range of addresses for multicast: every ethernet/FDDI frame with its destination in the range 01-00-5e-00-00-00 to 01-00-5e-ff-ff-ff (hex) contains data for a multicast group. The prefix 01-00-5e identifies the frame as multicast, the next bit is always 0 and so only 23 bits are left to the multicast address. As IP multicast groups are 28 bits long, the mapping can not be one-to-one. Only the 23 least significant bits of the IP multicast group are placed in the frame. The remaining 5 high-order bits are ignored, resulting in 32 different multicast groups being mapped to the same ethernet/FDDI address. This means that the ethernet layer acts as an imperfect filter, and the IP layer will have to decide whether to accept the datagrams the data-link layer passed to it. The IP layer acts as a definitive perfect filter.
Full details on IP Multicasting over FDDI are given in RFC 1390: Transmission of IP and ARP over FDDI Networks.

MBone
Using a new technology usually carries some advantages and disadvantages. The main disadvantage is that hundreds of hosts and, specially, routers don't support it yet. As a consequence, people who started working on multicast, bought new equipment, modified their operating systems, and built multicast islands in their local places. Then they discovered that it was difficult to communicate with people doing similar things because if only one of the routers between them didn't support multicast there was no way to get multicasting working between them.
The solution was clear: they decided to build a virtual multicast network in the top of the Internet. That is: sites with multicast routers between them could communicate directly. But sites joined across unicast routers would send their island's multicast traffic encapsulated in unicast packets to other multicast islands. Routers in the middle would not have problems, as they would be dealing with unicast traffic. Finally, in the receiving site, traffic would be de- encapsulated, and sent to the island in the original multicast way. Two ends converting from multicast to unicast, and then again to multicast define what is called a multicast tunnel.
The MBone or Multicast Backbone is that virtual multicast network based on multicast islands connected by multicast tunnels.
Several activities take place in the MBone daily, but it deserves to be remarked the profusion of tele-conferences with real time audio and video taking place across the whole Internet. For more Information on the MBone, see:
http://www.mediadesign.co.at/newmedia/more/mbone-faq.html

Multicast addresses are IP addresses in the range of 224.0.0.0 to 239.255.255.255.

There are some special multicast groups, say well known multicast groups, you should not use in your particular applications due the special purpose they are destined to:

In any case, range 224.0.0.0 through 224.0.0.255 is reserved for local purposes (as administrative and maintenance tasks) and datagrams destined to them are never forwarded by multicast routers. Similarly, the range 239.0.0.0 to 239.255.255.255 has been reserved for administrative scoping.
Hosts can be in three different levels of conformance with the Multicast specification, according to the requirements they meet.
Level 0:
is the no support for IP Multicasting level. Lots of hosts and routers in the Internet are in this state, as multicast support is not mandatory in IPv4 (it is, however, in IPv6). Not too much explanation is needed here: hosts in this level can neither send nor receive multicast packets. They must ignore the ones sent by other multicast capable hosts.
Level 1:
is the support for sending but not receiving multicast IP datagrams level. Thus, note that it is not necessary to join a multicast group to be able to send datagrams to it. Very few additions are needed in the IP module to make a Level 0 host Level 1-compliant.
Level 2:
is the full support for IP multicasting level. Level 2 hosts must be able to both send and receive multicast traffic. They must know the way to join and leave multicast groups and to propagate this information to multicast routers. Thus, they must include an Internet Group Management Protocol (IGMP) implementation in their TCP/IP stack.

Multicasting and linux
Linux is, full Level-2 Multicast-Compliant. It meets all requirements to send, receive and act as a router (mrouted) for multicast datagrams. Once the kernel is configured and installed, you should provide a default route for multicast traffic. The goal is to add a route to the network 224.0.0.0:
route add 224.0.0.0 netmask 240.0.0.0 dev eth0

Construction of multicast sockets
If we do not define a port number, the socket is bound to an anonymous port.


import sun.net.*;
import java.net.*;
import java.io.*;
 
public class MulticastConstructor  {
  public static void main(String[] args){
   try{
     MulticastSocket ms = new MulticastSocket(5500);
     System.out.println(ms);
    }
    catch (SocketException se) {
      System.err.println(se);
    }
    catch (IOException ie) {
     System.err.println(ie);
    }
  }
}

Once a MulticastSocket exists, there are four key operations it can perform:

  1. Join a multicast group
  2. Send data to the members of the group
  3. Receive data from the group
  4. Leave the multicast group
The MulticastSocket class has methods for operations 1, 2 and 4. No new method is necessary for receiving data.

public void joinGroup(InetAddress mcastaddr) throws SocketException;

public void leaveGroup(InetAddress mcastaddr) throws SocketException;

public synchronized void send(DatagramPacket dp, byte ttl) throws
SocketException, IOException;
Most multicast servers do not discriminate any clients. Therefore it is easy to join a group and watch the data that is being sent to it:

import sun.net.*;
import java.net.*;
import java.io.*;

public class MulticastSniffer{

    public static void main(String[] args) {
        InetAddress ia = null;
        byte[] buffer = new byte[65535];
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
        int port = 0;


        //read the address from the command line
        try{
            try {
                ia = InetAddress.getByName(args[0]);
            }
            catch (UnknownHostException e) {
                System.err.println(e);
            }
            port = Integer.parseInt(args[1]);
        }// end try
        catch (Exception e) {
            System.err.println(e);
            System.err.println
            ("Usage: java MulticastSniffer MulticastAddress port");
            System.exit(1);
        }

        try{
            MulticastSocket ms = new MulticastSocket(port);
            ms.joinGroup(ia);
            while (true) {
                ms.receive(dp);
                String s = new String(dp.getData(),0,dp.getLength());
                System.out.println(s);
            }
        }
        catch (SocketException se) {
            System.err.println(se);
        }
        catch (IOException ie) {
            System.err.println(ie);
        }
    }
}


next up previous contents
Next: Internet Up: Networking Previous: Writing a datagram client   Contents
Prof. H.P. Oser
2001-06-08