Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Multicasting

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multicasting

    Hi

    I have a code written for multicasting between client and server. On running this demo, The client receives only a few packets and some packets are dropped. Instead I want to ensure that all the packets are received using multicastsocket. Is it possible to implement TCP for connection establishment and then use multicastsocket. Do we have necessity of creating multiple threads for this..
    Pls HELP!!!!

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

    import javax.swing.*;
    public class QuoteMultiClient1
    {

    public static JButton B_CONNECT = new JButton("CONNECT");
    public static JFrame MainWindow=new JFrame("DatagamQuote");
    public static JScrollPane SP_OUTPUT=new JScrollPane();
    public static JTextArea TA_OUTPUT=new JTextArea();
    static int time=5000;

    public static void main(String[] args) throws Exception
    {

    BuildGui();

    }


    public static void CONNECT()
    {
    try
    {
    InetAddress IP_ADDRESS=InetAddress.getByName("224.0.0.0");
    byte[] bugf=new byte[256];
    //DatagramSocket dgsoc=new DatagramSocket(208,IP_ADDRESS);
    DatagramPacket PACKET2=new DatagramPacket(bugf,bugf.length);
    //dgsoc.receive(PACKET2);


    MulticastSocket SOCK=new MulticastSocket(208);
    //InetAddress IP_ADDRESS=InetAddress.getByName("224.0.0.0");
    SOCK.joinGroup(IP_ADDRESS);

    SOCK.receive(PACKET2);
    System.out.println("received control packet");
    DatagramPacket PACKET;


    //DatagramPacket ACK;
    for(int i=0;i<6;i++)
    {
    byte[] buf = new byte[600];
    PACKET = new DatagramPacket(buf,buf.length);
    SOCK.receive(PACKET);
    String MESSAGE=new String(PACKET.getData(),0,PACKET.getLength());
    TA_OUTPUT.append("\n \n Todays Quote" + MESSAGE);
    }
    //DatagramSocket sock=new DatagramSocket(209);
    byte[] bufack=new byte[256];
    String ack="Received few Quotes";
    bufack=ack.getBytes();
    DatagramPacket ackpkt=new DatagramPacket(bufack,bufack.length,IP_ADDRESS,108 0);
    SOCK.send(ackpkt);
    System.out.println("Client-end- ack sent");


    SOCK.leaveGroup(IP_ADDRESS);
    SOCK.close();
    }
    catch(IOException X)
    {
    System.out.println(X);
    }
    }
    public static void BuildGui()
    {
    MainWindow.setSize(410,280);
    MainWindow.setLocation(200,200);
    MainWindow.setResizable(false);
    MainWindow.setBackground(new java.awt.Color(255,255,255));
    MainWindow.getContentPane().setLayout(null);
    B_CONNECT.setBackground(new java.awt.Color(0,0,255));
    B_CONNECT.setForeground(new java.awt.Color(255,255,255));
    MainWindow.getContentPane().add(B_CONNECT);
    B_CONNECT.setBounds(150,15,110,25);
    TA_OUTPUT.setLineWrap(true);
    SP_OUTPUT.setHorizontalScrollBarPolicy(ScrollPaneC onstants.HORIZONTAL_SCROLLBAR_NEVER);
    SP_OUTPUT.setVerticalScrollBarPolicy(ScrollPaneCon stants.VERTICAL_SCROLLBAR_ALWAYS);
    SP_OUTPUT.setViewportView(TA_OUTPUT);
    MainWindow.getContentPane().add(SP_OUTPUT);
    SP_OUTPUT.setBounds(10,45,380,180);
    //MainWindow_Action();
    MainWindow.setVisible(true);
    MainWindow_Action();
    }

    public static void MainWindow_Action()
    {



    B_CONNECT.addActionListener(
    new java.awt.event.ActionListener()
    {

    public void actionPerformed(java.awt.event.ActionEvent evt)
    {


    CONNECT();
    }

    }




    );

    }
    }



    import java.io.*;
    import java.net.*;
    import java.util.*;
    public class QuoteMultiServer1 extends Thread {

    protected DatagramSocket SOCK=null;
    //protected DatagramSocket SOCK2=null;
    public int QuoteCount=0;
    public int Time_Interval=3000;
    public int Time_Interval2=5000;
    InetAddress IP_ADDRESS_GROUP =InetAddress.getByName("224.0.0.0");
    //InetAddress IP_CLIENT_ADDRESS=InetAddress.getByName("224.0.0.0 ");
    public String[] PirateQuote={ "dcsfffffffffffffffffff",
    "deeeeeeeeeew", "fhdfkhjhhhhhhhhhhhhhhhhhhhhhhh",
    "fdkjkfdghgdfgdfgfdg",
    "dsdjsfsdkfdfjlfklfkd;ff;;f",
    "shjfjshfskjfjhfjhdfhjhdfhdsfjh",
    "jhfsjhfsdjhfjsfjksfjksdfkjsdjk",
    "hehfehfhsfhdsjfjfjkdjkfjfjdjdfjkjfd",
    "dfsfsfsdsfsdfsdfsf"
    };
    public static void main(String[] args) throws Exception
    {
    QuoteMultiServer1 SERVER=new QuoteMultiServer1();
    SERVER.start();

    }


    public QuoteMultiServer1() throws IOException
    {
    SOCK=new DatagramSocket(1080);
    SOCK.connect(IP_ADDRESS_GROUP,208);
    //SOCK2=new DatagramSocket(2000);
    System.out.println("Launched Multicast Serve thread");
    System.out.println("Waiting fo client connection");
    }
    public void run()
    {
    while(true)
    {
    // System.out.println("ask client called");
    try
    {

    askclient();
    try{
    sleep(7000);
    }
    catch(InterruptedException X)
    {
    X.printStackTrace();
    }


    byte[] BUFFER=new byte[600];
    String MESSAGE=NextQuote();
    BUFFER=MESSAGE.getBytes();
    //InetAddress IP_ADDRESS_GROUP =InetAddress.getByName("224.0.0.0");
    DatagramPacket PACKET=new DatagramPacket(BUFFER,BUFFER.length,IP_ADDRESS_GRO UP,208);
    SOCK.send(PACKET);
    try
    {
    sleep(Time_Interval);
    }
    catch(InterruptedException X)
    {
    X.printStackTrace();
    }

    }
    catch(IOException X)
    {
    X.printStackTrace();
    break;
    }
    }
    try
    {
    //SOCK=new DatagramSocket(2000);
    byte[] bufffack= new byte[256];
    DatagramPacket clientackpkt=new DatagramPacket(bufffack,bufffack.length);

    SOCK.receive(clientackpkt);
    System.out.println("received ack");
    String ackmsg=new String(clientackpkt.getData());
    System.out.println("Client sent" + ackmsg);
    // SOCK.close();
    }
    catch(Exception e)
    {

    }


    SOCK.close();
    }


    protected String NextQuote()
    {
    String MESSAGE="";
    Date TheTime=new Date();
    if(QuoteCount >PirateQuote.length-1)
    {
    MESSAGE="\n fresh out quotes"+ TheTime.toString() + "";
    System.out.println("\n All quotes ae served ");

    /*try
    {
    SOCK=new DatagramSocket(2000);
    byte[] bufffack= new byte[256];
    DatagramPacket clientackpkt=new DatagramPacket(bufffack,bufffack.length);

    SOCK.receive(clientackpkt);
    System.out.println("received ack");
    String ackmsg=new String(clientackpkt.getData());
    System.out.println("Client sent" + ackmsg);
    // SOCK.close();
    }
    catch(Exception e)
    {

    }*/


    }
    else
    {
    MESSAGE=(QuoteCount + 1)+ "" + PirateQuote[QuoteCount];
    System.out.println("\n Serving out Quote#" + (QuoteCount+1) + "");
    QuoteCount++;
    }
    return MESSAGE;


    }

    public void askclient()
    {
    try
    {
    //System.out.println("inside askclient");
    byte[] BUFFER1=new byte[256];
    String MESSAGE1="ARE YOU COONECTED AND READY TO RECEIVE?";
    BUFFER1=MESSAGE1.getBytes();
    //InetAddress IP_ADDRESS_GROUP =InetAddress.getByName("224.0.0.0");
    DatagramPacket PACKET1=new DatagramPacket(BUFFER1,BUFFER1.length,IP_ADDRESS_G ROUP,208);



    SOCK.send(PACKET1);


    }

    catch(IOException ie)
    {

    }
    }

    }


  2. #2
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    My Mood
    Bored
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Multicasting

    In General, Multicasting is the concept of communication between one sender and multiple receivers over a network. It is packet type in IPv6. In java, multicast is the feature of UDP protocol. Over a particular IP address and port, an user can send message to multiple receivers.

Similar Threads

  1. Reliable Multicasting
    By usha in forum Java Networking
    Replies: 2
    Last Post: November 10th, 2013, 05:11 PM
  2. how to get ip address of systems in multicasting environment..??
    By omesh kumar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 11th, 2013, 12:38 PM