我正在开发一个tcp嗅探器项目。起初,我已经使用JCreator创建了系统,它没有任何界面就可以正常工作。接下来,我尝试使用NetBean 7.1为它创建用户界面。当然,我必须将代码分解为零件和零件,以适应按钮功能。似乎当我运行程序时,它不会产生任何输出,但会一直运行直到手动停止。它发生在我试图插入'打开设备'代码时。我怀疑导致问题的另一部分是'Thread.sleep(25);' part..Before插入那部分之前,我试图运行它产生输出,即框架,按钮等。
以下是该计划。非常需要我从这里得到的任何帮助。
感谢
import java.util.ArrayList;
import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.PacketReceiver;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;
public class NewJFrame extends javax.swing.JFrame {
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
static int deviceNum=0;
JpcapCaptor captor;
PacketStorage ps;
public NewJFrame() {
initComponents();
for(int i=0;i<devices.length;i++)
{
jComboBox1.addItem(""+devices[i].name);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jPanel1 = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton2.setText("Stop");
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addGap(22, 22, 22))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2))
.addGap(27, 27, 27))
);
jMenu1.setText("File");
jMenuItem1.setText("Run");
jMenu1.add(jMenuItem1);
jMenuItem3.setText("Stop");
jMenu1.add(jMenuItem3);
jMenuItem2.setText("Exit");
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
jMenu3.setText("About");
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 602, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
deviceNum = jComboBox1.getSelectedIndex();
try{
**captor=JpcapCaptor.openDevice(devices[deviceNum], 65535, true , 20);**
}
catch(Exception ex1){System.out.println("Cannot open device");}
ps = new PacketStorage();
new Thread(new Parser(ps)).start();
captor.loopPacket(-1, ps);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private static class PacketStorage implements PacketReceiver
{
private ArrayList<Packet> packets = new ArrayList<>();
public void receivePacket(Packet p)
{
packets.add(p);
}
public Packet getPacket()
{
return packets.size()>0?packets.remove(0):null;
}
}
private static class Parser implements Runnable
{
PacketStorage ps1;
public Parser(PacketStorage ps2)
{
this.ps1=ps2;
}
public void run()
{
Packet p;
TCPPacket tcp;
while(true)
{
p=ps1.getPacket();
if(p!=null && p instanceof TCPPacket )
{
tcp =(TCPPacket)p;
jTextArea1.append("Src: " + tcp.src_ip + ":" + tcp.src_port + " Dst: " + tcp.dst_ip + ":" + tcp.dst_port+"\n");
jTextArea1.append("Sequence Number Value: " + tcp.sequence + " Ack Number: " + tcp.ack_num+"\n");
}
try{
**Thread.sleep(25);**
}
catch(InterruptedException e){}
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private static javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
答案 0 :(得分:1)
部分你是对的,你有Concurency in Swing
的问题Swing是单线程的,所有输出到Swing GUI必须在EDT上完成
你运行冗长而繁重的任务,其中GUI的输出超出了EDT,包括声明的thread_safe方法,
您可以通过将JTextArea#append()包装到调用EDT的invokeLater()
中来解决这个问题
正确的方法是从SwingWorker调用此代码,方法publish()可以更新JTextArea
,输出应该在EDT上
答案 1 :(得分:1)
关于:
while(true)
{
p=ps1.getPacket();
if(p!=null && p instanceof TCPPacket )
{
tcp =(TCPPacket)p;
jTextArea1.append("Src: " + tcp.src_ip + ":" + tcp.src_port + " Dst: " + tcp.dst_ip + ":" + tcp.dst_port+"\n");
jTextArea1.append("Sequence Number Value: " + tcp.sequence + " Ack Number: " + tcp.ack_num+"\n");
}
try{
Thread.sleep(25);
}
catch(InterruptedException e){}
}
}
问题:你在哪里突破while (true)
循环?
答:不在哪里。
解决方案:解决此问题。