Multicast Programming with Java

06 Mar 2011 by

Multicast Overview

Multicast Overview

Multicast is a special feature of UDP protocol that enable programmer to send message to a group of receivers on a specific multicast IP address and port. Multicast has advantage in this scenario.

Let us say I want to send “Hello” message to 100 computers on my network. Perhaps, my first solution is to send the “Hello” message to each of them via UDP or TCP.

What a problem is this scenario?

There are 3 generic problems:

  • Consume a lot of processing power on sender as it needs to send to every receiver
  • Bandwidth flooding
  • The arrival time is not the same for every receiver

Seeing this problem, I propose my second solution by employing Multicast. Multicast runs over UDP protocol.

Multicast Principal

Multicast Network

Multicast Network

 

Multicast Characteristics

  • Multicast is using UDP under the hood. So sending and receiving data are much the same as UDP
  • The big noticeable from UDP
    • Sender should address packages to an IP number in the range between 224.0.0.1 and 239.255.255.254. Please see the full range of Multicast IP Address
    • Receivers must join multicast group to receive packet
  • Several multicast sockets can be bound simultaneously to the same port (Contrary to UDP and TCP)
  • Multicast is viable for video conference, service discovery application, etc.

Multicast in Java

  • MulticastSocket: extension of DatagramSocket
    • MulticastSocket socket = new MulticastSocket(8888);
  • Join and Leave group
    • joinGroup(InetAddress group)
    • leaveGroup(InetAddress group)

Example

Example below is to send multicast message with indexing to multicast IP (224.2.2.3) and port 8888. If the client wish to receive multicast message, it must join the group with multicast ip (224.2.2.3) and port 8888.

MulticastSender.java

import java.io.*;
import java.net.*;
/**
 * @author lycog
 */
public class MulticastSender {
  public static void main(String[] args) {
    DatagramSocket socket = null;
    DatagramPacket outPacket = null;
    byte[] outBuf;
    final int PORT = 8888;

    try {
      socket = new DatagramSocket();
      long counter = 0;
      String msg;

      while (true) {
        msg = "This is multicast! " + counter;
        counter++;
        outBuf = msg.getBytes();

        //Send to multicast IP address and port
        InetAddress address = InetAddress.getByName("224.2.2.3");
        outPacket = new DatagramPacket(outBuf, outBuf.length, address, PORT);

        socket.send(outPacket);

        System.out.println("Server sends : " + msg);
        try {
          Thread.sleep(500);
        } catch (InterruptedException ie) {
        }
      }
    } catch (IOException ioe) {
      System.out.println(ioe);
    }
  }
}

MulticastReceiver.java

import java.io.*;
import java.net.*;
/**
 *
 * @author lycog
 */
public class MulticastReceiver {
  public static void main(String[] args) {
    MulticastSocket socket = null;
    DatagramPacket inPacket = null;
    byte[] inBuf = new byte[256];
    try {
      //Prepare to join multicast group
      socket = new MulticastSocket(8888);
      InetAddress address = InetAddress.getByName("224.2.2.3");
      socket.joinGroup(address);

      while (true) {
        inPacket = new DatagramPacket(inBuf, inBuf.length);
        socket.receive(inPacket);
        String msg = new String(inBuf, 0, inPacket.getLength());
        System.out.println("From " + inPacket.getAddress() + " Msg : " + msg);
      }
    } catch (IOException ioe) {
      System.out.println(ioe);
    }
  }
}

 

Tags: ,

7 responses so far

Leave a Reply