如何从LAN端口读取数据并在applet上显示?

时间:2017-06-09 05:16:45

标签: java asp.net jar applet

基本上......当我从命令提示运行时,我可以从LAN端口读取数据。

javac XYZ.java
appletviewer XYZ.html

这适用于我的电脑。我可以读取数据并显示在我的applet上。

但是当我在ASPX页面上载我的applet并运行时。它是加载和运行。但它不是从端口读取的diplay数据。

这是我的代码:

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.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
                {
                    //1. creating a server socket, parameter is local port number
                    sock = new DatagramSocket(8001);

                    //buffer to receive incoming data
                    byte[] buffer = new byte[65536];
                    DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);

                    //2. Wait for an incoming data
                    System.out.println("Server socket created. Waiting for incoming data...");

                    //communication loop
                    while(true)
                    {
                        sock.receive(incoming);
                        byte[] data = incoming.getData();
                        String s = new String(data, 0, incoming.getLength());

                        //echo the details of incoming data - client ip : client port - client message
                        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() );


             }  
                        //s = "OK : " + s;
                        //DatagramPacket dp = new DatagramPacket(s.getBytes() , s.getBytes().length , incoming.getAddress() , incoming.getPort());
                        //sock.send(dp);
                        }
        }

                catch(IOException e)
        {
                    System.err.println("IOException " + e);
                }

    }
    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);  
        }   
    }


}

下面的图片是我从命令提示符输出的

enter image description here

enter image description here

以下是网络浏览器输出。

[在此输入图像说明] [3]

1 个答案:

答案 0 :(得分:0)

是。终于得到解决方案我在创建applet jar文件时犯了错误。 在R& d

最终成功获得结果。

http://www.pawlan.com/monica/articles/signedapps/

http://www.onjava.com/pub/a/onjava/2001/03/22/plugin.html?page=3

相关问题