0

I have one Device Machine. That is sending datagram packets LAN port via LAN cable RJ45 pin connected to my Computer.

I can get data when I am executing my code from command prompt. It is get proper output. But when i am using same applet load on my browser(Mozila FireFox) It is connect to my port ready to read my packets. but When i am sending data from device Machine to My computer. I created .java file, .class file, .jar file. Load my applet into HTML/ASPX page. nothing is happen. In Java console nothing is display. No error, No Exception. No data. Entire applet is get hanged.

So please help me. What should I do?

<HTML>
   <HEAD>
   </HEAD>
      <BODY>
      <div >
         <APPLET CODE = "FCApplet.class" WIDTH = "800" HEIGHT = "500"></APPLET>
      </div>
   </BODY>

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.io.*;
import java.lang.*;
import java.net.*;
public class FCApplet extends JApplet implements ActionListener , Runnable  
{
    Thread th = new Thread(this);
    TextField txtID,txtName,txtResult;
    Label lblID,lblName,lblResult;
    String MemberID;
    Panel p2;
    public void init()
    {
        setBackground(Color.decode("#BFBFBF"));
        lblID = new Label(" ID ");
        lblID.setFont(new Font("Verdana", Font.BOLD , 12));
        txtID= new TextField();

        lblName= new Label(" Name ");
        lblName.setFont(new Font("Verdana", Font.BOLD , 12));
        txtName= new TextField();

        lblResult= new Label(" Result ");
        lblResult.setFont(new Font("Verdana", Font.BOLD , 12));
        txtResult= new TextField();

        Button b = new Button("Connect");
        b.setFont(new Font("Verdana", Font.BOLD , 12)); 
        b.addActionListener(this);

        p2=new Panel();
                    p2.setLayout(new GridLayout(1,10,5,5));
        p2.setPreferredSize(new Dimension(900, 20));
                    p2.add(lblID);
                  p2.add(txtID);
                p2.add(lblName);
                  p2.add(txtName);
        p2.add(lblResult);
                  p2.add(txtResult);
                  p2.add(b);

        GridBagLayout gbl = new GridBagLayout();
        setLayout(gbl);
            GridBagConstraints c = new GridBagConstraints();
            c.anchor = GridBagConstraints.WEST;
            c.fill=GridBagConstraints.HORIZONTAL;
            c.insets = new Insets(10,10,10,10);
        c.fill=GridBagConstraints.HORIZONTAL; 
            c.gridy=1; 
            gbl.setConstraints(p2,c);     
        add(p2);
    }
    public void StartTest()
    {
        DatagramSocket sock = null;
        try
                {
                    sock = new DatagramSocket(8001);
                            byte[] buffer = new byte[1024];
                    DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
                                System.out.println("Server socket created. Waiting for incoming data...");

            try 
            {
            Thread.sleep(15000);
                        while(true)
                        {
                try 
                {
                 sock.receive(incoming);
                }
                catch(Exception ex) 
                {
                    System.err.println("IOException " + ex);
                }
                          byte[] data = incoming.getData();
                if(data != null)
                {
                    System.out.println("Data is not blank...");
                }
                else
                {
                    System.out.println("Data is blank...");
                }
                            String s = new String(data, 0, incoming.getLength());
                          System.out.println(incoming.getAddress().getHostAddress() + " : " + incoming.getPort() + " : " + s);

              if(s != "" )
             {

                String R = incoming.getAddress().getHostAddress() + " : " + incoming.getPort() + " : " + s;
                //lblResultTest.setText(R);
                String[] words = s.split("&");

                txtResult.setText(words[2]);
                this.getAppletContext().showDocument( this.getDocumentBase() );
            }   

                        }
        }
        catch(InterruptedException ex) 
        {
            Thread.currentThread().interrupt();
        }

        }

                catch(IOException e)
        {
                      System.err.println("IOException " + e);
            System.out.print("Exception: ");
            System.out.println(e.getMessage());
                }
    }
    public void run() 
    {
        try 
        {
            Thread.sleep(1000);
        }
        catch(InterruptedException ex) 
        {
            Thread.currentThread().interrupt();
        }
    }
    public void actionPerformed(ActionEvent e)
    {
        Button source = (Button)e.getSource();
        if(source.getLabel() == "Connect")
        {
            StartTest(); 
        }
        else{
            JOptionPane.showMessageDialog(null,"Please Provide Input","alert",JOptionPane.WARNING_MESSAGE);  
        }   
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
PTank
  • 71
  • 1
  • 7
  • Have you read this? https://stackoverflow.com/questions/868111/how-do-you-debug-java-applets – Stephen C Jun 12 '17 at 13:24
  • My guess is that your attempt to create a socket or receive a packet or something like that is causing a SecurityException. Have you checked to see if anything is being reported in the browser plugin's Java console? – Stephen C Jun 12 '17 at 13:29
  • 1) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. – Andrew Thompson Jun 13 '17 at 03:08
  • As an aside: `` this won't work in a modern JVM. Before browsers started removing the plug-in completely, (Sun/)Oracle decided that all applets need to be digitally signed, which means the applet has to be in a Jar (before it can be signed). – Andrew Thompson Jun 13 '17 at 03:30
  • @StephenC I was visited that link. But When i press any key listing in java console in running mode. Nothing is happen. It feels like applet is hang. – PTank Jun 13 '17 at 05:03
  • and Yes, your guess is right, I checked in java console nothing any exception throw. `Server socket created. Waiting for incoming data...` This line is display after that nothing is happen. I putted one line under while loop so it is also printing, It means it is start reading the port but i don't Know Why data is not display on my applet. @StephenC – PTank Jun 13 '17 at 05:11
  • This is for my testing purpose after i ll convert my to Swing. But does it is make any difference to read datagram packets and display in Textbox?@AndrewThompson – PTank Jun 13 '17 at 05:18
  • Have you checked the firewall settings? But AndrewThompson is right. Don't use applets. Don't use AWT. Use Swing now. There is no point in battling with applets and applet-related problems if they are only "for testing". – Stephen C Jun 13 '17 at 07:17
  • Yes i checked Firewall. it is not blocking. Is their any specific changes than please tell me. so i ll do that. and Yes i ll change AWT to swing. @StephenC – PTank Jun 13 '17 at 08:52

0 Answers0