我有一台设备机器。即通过连接到我的电脑的LAN电缆RJ45引脚发送数据报包LAN端口。
我从命令提示符执行代码时可以获取数据。得到适当的输出。但是当我在我的浏览器上使用相同的applet加载(Mozila FireFox)它连接到我的端口准备读取我的数据包。但是当我从设备机器向我的计算机发送数据时。我创建了.java文件,.class文件,.jar文件。将我的applet加载到HTML / ASPX页面。什么都没发生。在Java控制台中没有显示任何内容没有错误,没有例外。没有数据。整个小程序被绞死。
所以请帮助我。我该怎么办?
<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);
}
}
}