2012-02-04 108 views
-2

即时通讯工作在一个TCP嗅探器项目。起初,我已经使用JCreator创建了系统,并且它没有任何接口就可以正常工作。接下来,我试图使用NetBean 7.1为它创建用户界面。当然,我必须将代码分解成零件和零件,以便装入按钮功能中。看来,当我运行该程序时,它不会产生任何输出,而是一直运行直到手动停止。它发生在我试图插入'开放设备'代码时。另一个我怀疑造成问题的部分是'Thread.sleep(25);'部分..在插入那部分之前,我试着运行它产生的输出是框架,按钮等等。tcp嗅探器不产生输出

下面是程序。非常需要我从这里得到的任何帮助。

感谢

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     
} 

回答

1

部分你是对的,你有问题,与Concurency in Swing

  • Swing是单线程的,所有输出的Swing GUI的必须在美国东部时间

  • 完成

    您输出到GUI的输出超出了EDT,包括声明的thread_safe方法,您运行的任务长且繁重,

  • 你可以简单的解决通过包装JTextArea#append()invokeLater()是调用EDT

  • 正确的方法是从SwingWorker调用此代码,从方法publish()可以更新JTextArea和输出应该在美国东部时间

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)循环吗?
答:没有的地方。
解决方法:解决这个问题。

+0

@din:如果你仍然卡住了,请在这里回复评论,我可以帮助你进一步。 – 2012-02-04 17:58:52