?
Solved

Multicast messages in applet

Posted on 2007-10-12
4
Medium Priority
?
435 Views
Last Modified: 2008-09-01
I have an applet, that listenes to multicast messages - when I try to run both on one single machine they work.... the server(one that sends messages) and the applet -

but when i use different machines it doesnt work - here is the code
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;

public class MultiCast extends JApplet {

    JTextArea display;

    public void init() {
        //Execute a job on the event-dispatching thread:
        //creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't successfully complete");
        }
    }
   
    private void createGUI() {
        //Create the text area and make it uneditable.
          display = new JTextArea(1, 80);
        display.setEditable(false);

            //Set the layout manager so that the text area
            //will be as wide as possible.
        setLayout(new GridLayout(1,0));

            //Add the text area (in a scroll pane) to the applet.
        add(new JScrollPane(display));
       
        }

    public void start() {
                new server();
    }

    public void stop() {

    }

    public void destroy() {        
        //Execute a job on the event-dispatching thread:
        //destroying this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    getContentPane().removeAll();                    
                }
            });
        } catch (Exception e) { }
    }
   
    void addItem(String newWord, boolean onEDT) {
        final String s = newWord + "\n";
       
        System.out.println(newWord);
       
        if (onEDT) {
            display.append(s);
        } else {
            //Execute a job on the event-dispatching thread:
            //updating this applet's GUI.
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        display.append(s);
                    }
                });
            } catch (Exception e) { }
        }
    }
      
      public class server extends Thread {
      
            public server(){
                  start();
            }
            
            public void run() {
            try{
                  MulticastSocket socket = new MulticastSocket(9123);
                  InetAddress address =  InetAddress.getByName("225.0.0.10");                  
                  InetAddress addressHost =  InetAddress.getLocalHost();
                  
                     socket.joinGroup(address);
      
                     DatagramPacket packet;
                     byte message[] = new byte[1024];
      
                     packet = new DatagramPacket(message, message.length);
                     while(true){
                        try{
                              Thread.sleep(1000);
                        } catch(Exception ex){}
                        
                        addItem("Reading", false);
                        socket.receive(packet);
                           String time = new String(packet.getData());
                           addItem(time, false);
                  }
            }
            catch(IOException e){}
         /*
                  for(;;){
                         addItem("vivian ", false); //not on event-dispatching thread
                         try{
                              Thread.sleep(2000);
                        }
                        catch(Exception e){}
                   }*/
            }
      }
}

0
Comment
Question by:Vivian Aranha
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 20068380
An applet cannot ordinarily create a socket unless it's signed. If you look at the Java console error messages you'll probably see a security exception
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 20069304
>             catch(IOException e){}

firstly, don't ignore exceptions. You'll never know whats going wrong.
at a minimum use:

            catch(IOException e){
                 e.printStackTrace();
             }
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question