Posted by : Unknown Friday 26 June 2015


Multicast Sender

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

class MulticastSender implements Runnable
{
    static int port;
    static MulticastSocket ms;
    static String group;
    static int status = 0 ;
    MulticastSender() throws Exception
    {
        port = 5000;
        group = "225.4.5.6";
        ms = new MulticastSocket(4000);
    }
    public void run()
    {
        while(true)
        {
            try
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                byte sbuf[] = new byte[2000];
                sbuf = br.readLine().getBytes();
                DatagramPacket spack = new DatagramPacket(sbuf, sbuf.length,
                                                 InetAddress.getByName(group), port);
                ms.send(spack);                                                
               
            }catch(Exception e){ System.out.println(e);}
        }
    }
    public static void main(String args[]) throws Exception
    {
        MulticastSender sender = new MulticastSender();
        Thread t = new Thread(sender);
               
        ms.joinGroup(InetAddress.getByName(group));
        t.start();
       
        int count =0;
        byte rbuf[] = new byte[1000];
        while(true)
        {
            DatagramPacket rpack =new DatagramPacket(rbuf,rbuf.length);
            ms.receive(rpack);
            String str = new String(rpack.getData(),0,rpack.getLength());
            if(str.equalsIgnoreCase("join"))
            {
                count++;
                System.out.println("Client joined the group");
                System.out.println("\ncurrently " + count + " client/clients is/are in the group");
               
            }
            else if(str.equalsIgnoreCase("leave"))
            {
                count--;
                System.out.println("Client left the group");
                System.out.println("\ncurrently " + count + " client/clients is/are in the group");
                if(count == 0)
                {
                    ms.leaveGroup(InetAddress.getByName(group));
                    ms.close();
                    break;
                }
            }
        }
    }
}

Multicast Receiver

import java.io.*;
import java.net.*;
class MulticastReceiver implements Runnable
{
    static MulticastSocket ms;
    static int port;
    static int status = 0;
    MulticastReceiver() throws Exception
    {
        port = 5000;
        ms= new MulticastSocket(port);
    }
    public void run()
    {
        while(true)
        {
            try
            {
                byte rbuf[] = new byte[2000];
                DatagramPacket  rpack = new DatagramPacket(rbuf,rbuf.length);
                if (status == 1)
                {
                    ms.close();
                    break;
                }
                ms.receive(rpack);
                System.out.println(new String(rpack.getData(),0,rpack.getLength()));
            }catch(Exception e){System.out.println(e);}
        }
    }
   
    public static void main(String args[]) throws Exception
    {
        MulticastReceiver mr =new MulticastReceiver();
        Thread t = new Thread(mr);
       
        String group = "225.4.5.6";
      
        System.out.println("Do you wish to join the group?");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        if(br.readLine().equalsIgnoreCase("yes"))
        {
            ms.joinGroup(InetAddress.getByName(group));
           
            byte sbuf[] = new byte[2000];
            sbuf = "join".getBytes();
            DatagramPacket spack = new DatagramPacket(sbuf, sbuf.length,InetAddress.getByName("localhost"),4000);
            ms.send(spack);
          
            System.out.println("Joined the group");
            t.start();
            while(true)
            {
                if(br.readLine().equalsIgnoreCase("leave"))
                {
                    status = 1;
                    byte sbuf2[] = new byte[2000];
                    sbuf2 = "leave".getBytes();
                    DatagramPacket spack2 = new DatagramPacket(sbuf2,sbuf2.length,InetAddress.getByName("localhost"),4000);
                    ms.send(spack2);
                    ms.leaveGroup(InetAddress.getByName(group));
                    break;
                }
            }
        }
    }
}

How to run:

  1. First run Receiver program 
  2. Then next run Sender program in single or multiple times
  3. Give input "yes" from Receiver side
  4. Next give input in Sender side

Receiver Side Output:

Do you wish to join the group?
yes
Joined the group
hi

Sender Side Output:

Client joined the group

currently 1 client/clients is/are in the group
hi

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Welcome to My Blog

Translate

Popular Post

Total Pageviews

Blog Archive

- Copyright © Learning Java Program - Powered by Blogger -