tcp 嗅探器不产生输出
我正在从事 TCP 嗅探器项目。首先,我使用 JCreator 创建了系统,并且无需任何界面即可正常工作。接下来,我尝试使用 NetBean 7.1 创建用户界面。当然,我必须将代码分解成多个部分才能适合按钮功能。似乎当我运行该程序时,它不会产生任何输出,但会继续运行直到手动停止。当我尝试插入“打开设备”代码时会发生这种情况。我怀疑导致问题的另一部分是“Thread.sleep(25);”部分..在插入该部分之前,我尝试运行它并生成框架、按钮等输出。
下面是程序。非常需要我能从这里得到的任何帮助。
谢谢
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
}
im working on a tcp sniffer project. at first, ihave created the system using JCreator and it works fine without any interface. Next, im trying to create the user interface for it using NetBean 7.1. of course i have to break the code into parts and pieces to be fitted into the button function. It seems that when i run the program, it doesn't produce any output but keeps on running till manually stopped. it happen when i tried to insert the 'open device' code. another part that i suspected to cause the problem is 'Thread.sleep(25);' part..Before inserting that part, i tried to run and it produces the output which is the frame, button, etc..
Below are the program. badly need any help that i can get from here.
thanks
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
部分你是对的,你遇到了 Swing 中的并发 问题
Swing 是单线程的,所有到 Swing GUI 的输出都必须在 EDT 上完成
您正在运行又长又繁重的任务,其中输出到图形用户界面超出了 EDT,包括声明的 thread_safe 方法,
您可以通过包装 JTextArea#append() 到调用 EDT 的
invokeLater()
中,
正确的方法是从 SwingWorker,来自方法 发布() 您可以更新
JTextArea
并且输出应该在 EDTpartially you are right, you have got issue with Concurency in Swing
Swing is single threaded and all output to the Swing GUI must be done on EDT
you running long and heavy task where output to the GUI is out of EDT, including declared thread_safe methods,
you can simple solve that by wrapping JTextArea#append() into
invokeLater()
that invoking EDTcorrect way would be invoke this code from SwingWorker, from method publish() you can update
JTextArea
and output should be on EDT关于:
问题:您在哪里打破
while (true)
循环?答案:没有。
解决方案:解决这个问题。
Regarding:
Question: Where do you break out of your
while (true)
loop?Answer: no where.
Solution: fix this.