基于java的网络抓包技术研究与实现

本实验是用java实现的网络抓包程序,在windows环境下安装winpcap4.0和jpcap6.0后,下载eclipse和jigloo插件(一种在eclipse底下作图形化开发的工具),将其安装好,然后就可以进行java的网络抓包图形化开发了。

二、原理与关键技术
2.1 网络抓包技术原理
网络层上有各种各样的数据包,它们以不同的帧格式在网络层上进行传输,但是在传输时它们都遵循相同的格式,即有相同的长度,如果一种协议的帧格式达不到这种长度,就让其补齐,以达到我们的要求。

2.2 网络抓包关键技术
无论是在windows操作系统下还是在linux操作系统下,要想捕获网络上的数据包,必须要对网卡进行控制,因为本机的数据报从网络上来到本机是通过网卡然后再保存到本地缓冲区上的,所以要抓获网包就必须调用网卡驱动中的对外函数,在linux系统中有net.h文件,可以调用net.h文件中的函数来操作网卡,可以直接编程实现,但为了更方便的使用,可以安装一个叫libpcap的软件,这样调用函数更好用,而在windows系统中,因为源代码不对外公开,所以要安装一个叫winpcap的软件,这样用C或VC++就可以实现了,但因为我用的是java语言来实现的,所以无论是在哪个系统都要安装一个叫jpcap的软件,它本身就把底层的函数又封装了一下,这样就可以让java来使用了。

三、设计与实现
3.1 基于java的设计方案
我的这个网络抓包程序是图形化操作界面,在菜单栏点击抓包按钮后选择网卡和过滤字还有最长字长,点击开始,然后就可以开始抓包了,在主界面中就会显示出一行又一行的数据,这些数据就是抓获到的数据包。

3.2 具体实现
1、安装winpcap4.0和jpcap6.0

2、下载eclipse3.3和jigloo,jigloo是eclipse底下的插件,是用来支持eclipse底下的java 图形化开发的。

3、编写java抓包程序:

建立三个文件,一个主程序,一个抓包程序,一个抓包选项程序对话框程序

第一个程序:主程序如下Java代码  

收藏代码
  1. package netcap;  
  2.   
  3. import java.awt.event.ActionEvent;  
  4.   
  5. import java.awt.event.ActionListener;  
  6.   
  7.    
  8.   
  9. import javax.swing.JSeparator;  
  10.   
  11. import javax.swing.JMenuItem;  
  12.   
  13. import javax.swing.JMenu;  
  14.   
  15. import javax.swing.JMenuBar;  
  16.   
  17.    
  18.   
  19. import java.awt.*;  
  20.   
  21. import java.awt.event.*;  
  22.   
  23. import javax.swing.*;  
  24.   
  25. import javax.swing.table.*;  
  26.   
  27. import netcap.*;  
  28.   
  29. import jpcap.*;  
  30.   
  31. import jpcap.packet.*;  
  32.   
  33. import java.util.*;  
  34.   
  35. import java.sql.Timestamp;  
  36.   
  37.    
  38.   
  39. /** 
  40.  
  41. * This code was edited or generated using CloudGarden’s Jigloo 
  42.  
  43. * SWT/Swing GUI Builder, which is free for non-commercial 
  44.  
  45. * use. If Jigloo is being used commercially (ie, by a corporation, 
  46.  
  47. * company or business for any purpose whatever) then you 
  48.  
  49. * should purchase a license for each developer using Jigloo. 
  50.  
  51. * Please visit www.cloudgarden.com for details. 
  52.  
  53. * Use of Jigloo implies acceptance of these licensing terms. 
  54.  
  55. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR 
  56.  
  57. * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED 
  58.  
  59. * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. 
  60.  
  61. */  
  62.   
  63. public class JFrameMain extends javax.swing.JFrame implements ActionListener{  
  64.   
  65.    
  66.   
  67.        private JMenuItem exitMenuItem;  
  68.   
  69.        private JSeparator jSeparator2;  
  70.   
  71.        private JMenuItem saveAsMenuItem;  
  72.   
  73.        private JMenuItem saveMenuItem;  
  74.   
  75.        private JMenuItem stopMenuItem;  
  76.   
  77.        private JMenuItem startMenuItem;  
  78.   
  79.        private JMenu Menu;  
  80.   
  81.        private JMenuBar jMenuBar1;  
  82.   
  83.          
  84.   
  85.        JTable tabledisplay = null;  
  86.   
  87.        Vector rows,columns;  
  88.   
  89.        DefaultTableModel tabModel;  
  90.   
  91.        JScrollPane scrollPane;  
  92.   
  93.        JLabel statusLabel;  
  94.   
  95.          
  96.   
  97.        Netcaptor captor = new Netcaptor();  
  98.   
  99.    
  100.   
  101.        /** 
  102.  
  103.        * Auto-generated main method to display this JFrame 
  104.  
  105.        */  
  106.   
  107.        public static void main(String[] args) {  
  108.   
  109.               JFrameMain inst = new JFrameMain();  
  110.   
  111.               inst.setVisible(true);  
  112.   
  113.        }  
  114.   
  115.          
  116.   
  117.        public JFrameMain() {  
  118.   
  119.               super();  
  120.   
  121.               initGUI();  
  122.   
  123.        }  
  124.   
  125.          
  126.   
  127.        private void initGUI() {  
  128.   
  129.               try {  
  130.   
  131.                      setSize(400, 300);  
  132.   
  133.                      {  
  134.   
  135.                             jMenuBar1 = new JMenuBar();  
  136.   
  137.                             setJMenuBar(jMenuBar1);  
  138.   
  139.                             {  
  140.   
  141.                                    Menu = new JMenu();  
  142.   
  143.                                    jMenuBar1.add(Menu);  
  144.   
  145.                                    Menu.setText(“\u6293\u5305”);  
  146.   
  147.                                    Menu.setPreferredSize(new java.awt.Dimension(35, 21));  
  148.   
  149.                                    {  
  150.   
  151.                                           startMenuItem = new JMenuItem();  
  152.   
  153.                                           Menu.add(startMenuItem);  
  154.   
  155.                                           startMenuItem.setText(“开始”);  
  156.   
  157.                                           startMenuItem.setActionCommand(“start”);  
  158.   
  159.                                           startMenuItem.addActionListener(this);  
  160.   
  161.                                    }  
  162.   
  163.                                    {  
  164.   
  165.                                           stopMenuItem = new JMenuItem();  
  166.   
  167.                                           Menu.add(stopMenuItem);  
  168.   
  169.                                           stopMenuItem.setText(“停止”);  
  170.   
  171.                                           stopMenuItem.setActionCommand(“stop”);  
  172.   
  173.                                           stopMenuItem.addActionListener(this);  
  174.   
  175.                                    }  
  176.   
  177.                                    {  
  178.   
  179.                                           saveMenuItem = new JMenuItem();  
  180.   
  181.                                           Menu.add(saveMenuItem);  
  182.   
  183.                                           saveMenuItem.setText(“保存”);  
  184.   
  185.                                    }  
  186.   
  187.                                    {  
  188.   
  189.                                           saveAsMenuItem = new JMenuItem();  
  190.   
  191.                                           Menu.add(saveAsMenuItem);  
  192.   
  193.                                           saveAsMenuItem.setText(“保存为 …”);  
  194.   
  195.                                    }  
  196.   
  197.                                    {  
  198.   
  199.                                           jSeparator2 = new JSeparator();  
  200.   
  201.                                           Menu.add(jSeparator2);  
  202.   
  203.                                    }  
  204.   
  205.                                    {  
  206.   
  207.                                           exitMenuItem = new JMenuItem();  
  208.   
  209.                                           Menu.add(exitMenuItem);  
  210.   
  211.                                           exitMenuItem.setText(“Exit”);  
  212.   
  213.                                           exitMenuItem.setActionCommand(“exit”);  
  214.   
  215.                                           exitMenuItem.addActionListener(this);  
  216.   
  217.                                    }  
  218.   
  219.                             }  
  220.   
  221.                      }  
  222.   
  223.                        
  224.   
  225.                      rows=new Vector();  
  226.   
  227.                      columns= new Vector();  
  228.   
  229.                        
  230.   
  231.                      columns.addElement(“数据报时间”);  
  232.   
  233.                      columns.addElement(“源IP地址”);  
  234.   
  235.                      columns.addElement(“目的IP地址”);  
  236.   
  237.                      columns.addElement(“首部长度”);  
  238.   
  239.                      columns.addElement(“数据长度”);  
  240.   
  241.                      columns.addElement(“是否分段”);  
  242.   
  243.                      columns.addElement(“分段偏移量”);  
  244.   
  245.                      columns.addElement(“首部内容”);  
  246.   
  247.                      columns.addElement(“数据内容”);  
  248.   
  249.    
  250.   
  251.                        
  252.   
  253.                      tabModel=new DefaultTableModel();  
  254.   
  255.                      tabModel.setDataVector(rows,columns);  
  256.   
  257.                      tabledisplay = new JTable( tabModel );  
  258.   
  259.                      scrollPane= new JScrollPane(tabledisplay);  
  260.   
  261.                      this.getContentPane().add( new JScrollPane(tabledisplay),BorderLayout.CENTER);  
  262.   
  263.                        
  264.   
  265.                      statusLabel=new JLabel(“06610班 张琛雨 066100583”);  
  266.   
  267.                      this.getContentPane().add(statusLabel,BorderLayout.SOUTH);  
  268.   
  269.               } catch (Exception e) {  
  270.   
  271.                      e.printStackTrace();  
  272.   
  273.               }  
  274.   
  275.        }  
  276.   
  277.          
  278.   
  279.        public void actionPerformed(ActionEvent event){  
  280.   
  281.               String cmd=event.getActionCommand();  
  282.   
  283.                 
  284.   
  285.               if(cmd.equals(“start”)){  
  286.   
  287.                      captor.capturePacketsFromDevice();  
  288.   
  289.                      captor.setJFrame(this);  
  290.   
  291.               }  
  292.   
  293.               else if(cmd.equals(“stop”)){  
  294.   
  295.                      captor.stopCapture();  
  296.   
  297.               }  
  298.   
  299.               else if(cmd.equals(“exit”)){  
  300.   
  301.                      System.exit(0);  
  302.   
  303.               }  
  304.   
  305.        }  
  306.   
  307.    
  308.   
  309.        public void dealPacket( Packet packet )  
  310.   
  311.        {  
  312.   
  313.               try  
  314.   
  315.               {  
  316.   
  317.                      Vector r=new Vector();  
  318.   
  319.                      String strtmp;  
  320.   
  321.                      Timestamp timestamp = new Timestamp((packet.sec * 1000) + (packet.usec / 1000));   
  322.   
  323.                        
  324.   
  325.                      r.addElement( timestamp.toString() );  //数据报时间  
  326.   
  327.                      r.addElement(((IPPacket)packet).src_ip.toString());    //源IP地址  
  328.   
  329.                      r.addElement(((IPPacket)packet).dst_ip.toString());    //目的IP地址  
  330.   
  331.                      r.addElement( packet.header.length );   //首部长度  
  332.   
  333.                      r.addElement( packet.data.length );             //数据长度  
  334.   
  335.                      r.addElement( ((IPPacket)packet).dont_frag == true ? “分段” : “不分段” );                          //是否不分段  
  336.   
  337.                      r.addElement( ((IPPacket)packet).offset );          //数据长度  
  338.   
  339.          
  340.   
  341.                      strtmp = “”;  
  342.   
  343.                      for(int i=0;i<packet.header.length;i++){               
  344.   
  345.                             strtmp += Byte.toString(packet.header[i]);  
  346.   
  347.                      }  
  348.   
  349.                      r.addElement(strtmp);    //首部内容  
  350.   
  351.          
  352.   
  353.                      strtmp = “”;  
  354.   
  355.                      for(int i=0;i<packet.data.length;i++){            
  356.   
  357.                             strtmp += Byte.toString(packet.data[i]);  
  358.   
  359.                      }  
  360.   
  361.                      r.addElement(strtmp);    //数据内容  
  362.   
  363.                                                           
  364.   
  365.                      rows.addElement(r);  
  366.   
  367.                      tabledisplay.addNotify();  
  368.   
  369.               }  
  370.   
  371.               catch( Exception e)  
  372.   
  373.               {  
  374.   
  375.                        
  376.   
  377.               }  
  378.   
  379.        }  
  380.   
  381. }  
  382.   
  383. 在这里定义了一个向量r,当有数据包产生时,便将数据包赋值给r,rows.AddElement(r)语句便将r添加到向量rows中,然后tabledisplay.addNotify();语句就会刷新界面将新的数据包显示出来。  
  384.   
  385.    
  386.   
  387. 第二个程序:抓包  
  388.   
  389. package netcap;  
  390.   
  391.    
  392.   
  393. import java.io.File;  
  394.   
  395. import java.util.Vector;  
  396.   
  397.    
  398.   
  399. import javax.swing.JFileChooser;  
  400.   
  401. import javax.swing.JOptionPane;  
  402.   
  403.    
  404.   
  405. import jpcap.JpcapCaptor;  
  406.   
  407. import jpcap.PacketReceiver;  
  408.   
  409. import jpcap.JpcapWriter;  
  410.   
  411. import jpcap.packet.Packet;  
  412.   
  413.    
  414.   
  415. public class Netcaptor {  
  416.   
  417.    
  418.   
  419.        JpcapCaptor jpcap = null;  
  420.   
  421.        JFrameMain frame;  
  422.   
  423.          
  424.   
  425.        public void setJFrame(JFrameMain frame){  
  426.   
  427.               this.frame=frame;  
  428.   
  429.        }  
  430.   
  431.          
  432.   
  433.        public void capturePacketsFromDevice() {  
  434.   
  435.    
  436.   
  437.               if(jpcap!=null)  
  438.   
  439.                      jpcap.close();  
  440.   
  441.                        
  442.   
  443.               jpcap = Jcapturedialog.getJpcap(frame);  
  444.   
  445.                 
  446.   
  447.               if (jpcap != null) {  
  448.   
  449.                      startCaptureThread();  
  450.   
  451.               }  
  452.   
  453.    
  454.   
  455.        }  
  456.   
  457.          
  458.   
  459.        private Thread captureThread;  
  460.   
  461.          
  462.   
  463.        private void startCaptureThread(){  
  464.   
  465.                 
  466.   
  467.               if(captureThread != null)  
  468.   
  469.                      return;  
  470.   
  471.               captureThread = new Thread(new Runnable(){  
  472.   
  473.                      public void run(){  
  474.   
  475.                             while(captureThread != null){  
  476.   
  477.                                    jpcap.processPacket(1, handler);  
  478.   
  479.                             }  
  480.   
  481.                      }  
  482.   
  483.               });  
  484.   
  485.               captureThread.setPriority(Thread.MIN_PRIORITY);  
  486.   
  487.               captureThread.start();  
  488.   
  489.        }  
  490.   
  491.          
  492.   
  493.        void stopcaptureThread(){  
  494.   
  495.               captureThread = null;  
  496.   
  497.        }  
  498.   
  499.          
  500.   
  501.        public void stopCapture(){  
  502.   
  503.               System.out.println(2);  
  504.   
  505.               stopcaptureThread();  
  506.   
  507.        }  
  508.   
  509.          
  510.   
  511.        private PacketReceiver handler=new PacketReceiver(){  
  512.   
  513.               public void receivePacket(Packet packet) {  
  514.   
  515.                      //System.out.println(packet);  
  516.   
  517.                      frame.dealPacket(packet);  
  518.   
  519.               }  
  520.   
  521.                 
  522.   
  523.        };  
  524.   
  525. }  

定义一个抓包对象JpcapCaptor jpcap = null;但点击开始时调用private void startCaptureThread()方法开始抓包,jpcap.processPacket(1, handler);语句能够反复调用handler所指向的方法,这个方法中定义的packet就是网络上抓到的数据包,经过frame.dealPacket(packet);就可以显示在主界面上。

程序三:抓包选项Java代码  

收藏代码
  1. package netcap;  
  2.   
  3.    
  4.   
  5. import javax.swing.JFrame;  
  6.   
  7. import jpcap.*;  
  8.   
  9. import java.awt.*;  
  10.   
  11. import java.awt.event.*;  
  12.   
  13. import javax.swing.*;  
  14.   
  15.    
  16.   
  17.    
  18.   
  19. /** 
  20.  
  21. * This code was edited or generated using CloudGarden’s Jigloo 
  22.  
  23. * SWT/Swing GUI Builder, which is free for non-commercial 
  24.  
  25. * use. If Jigloo is being used commercially (ie, by a corporation, 
  26.  
  27. * company or business for any purpose whatever) then you 
  28.  
  29. * should purchase a license for each developer using Jigloo. 
  30.  
  31. * Please visit www.cloudgarden.com for details. 
  32.  
  33. * Use of Jigloo implies acceptance of these licensing terms. 
  34.  
  35. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR 
  36.  
  37. * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED 
  38.  
  39. * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. 
  40.  
  41. */  
  42.   
  43. public class Jcapturedialog extends javax.swing.JDialog implements ActionListener {  
  44.   
  45.    
  46.   
  47.        /** 
  48.  
  49.        * Auto-generated main method to display this JDialog 
  50.  
  51.        */  
  52.   
  53.        static JpcapCaptor jpcap=null;  
  54.   
  55.        private JRadioButton wholeRadioButton;  
  56.   
  57.        private JPanel buttonPanel;  
  58.   
  59.        private JButton cancelButton;  
  60.   
  61.        private JButton okButton;  
  62.   
  63.        private JRadioButton userRadioButton;  
  64.   
  65.        private JRadioButton headRadioButton;  
  66.   
  67.        private JPanel netPanel;  
  68.   
  69.        private JTextField caplenTextField;  
  70.   
  71.        private JPanel caplenPanel;  
  72.   
  73.        private JTextField TextField;  
  74.   
  75.        private JPanel filterPanel;  
  76.   
  77.        private JCheckBox CheckBox;  
  78.   
  79.        private JComboBox netJComboBox;  
  80.   
  81.        private JPanel jPanel_east;  
  82.   
  83.        private JPanel jPanel_west;  
  84.   
  85.    
  86.   
  87.        NetworkInterface[] devices;  
  88.   
  89.          
  90.   
  91.        public static void main(String[] args) {  
  92.   
  93.               JFrame frame = new JFrame();  
  94.   
  95.               Jcapturedialog inst = new Jcapturedialog(frame);  
  96.   
  97.               inst.setVisible(true);  
  98.   
  99.        }  
  100.   
  101.          
  102.   
  103.        public Jcapturedialog(JFrame frame) {  
  104.   
  105.               super(frame,”选择要检测的网卡并设置参数”,true);  
  106.   
  107.    
  108.   
  109.               try {  
  110.   
  111.                      BoxLayout thisLayout = new BoxLayout(  
  112.   
  113.                             getContentPane(),  
  114.   
  115.                             javax.swing.BoxLayout.X_AXIS);  
  116.   
  117.                      getContentPane().setLayout(thisLayout);  
  118.   
  119.                      {  
  120.   
  121.                             jPanel_west = new JPanel();  
  122.   
  123.                             jPanel_west.setLayout(new BoxLayout(jPanel_west,BoxLayout.Y_AXIS));  
  124.   
  125.                             getContentPane().add(jPanel_west);  
  126.   
  127.                             {  
  128.   
  129.                                    netPanel = new JPanel();  
  130.   
  131.                                    FlowLayout netPanelLayout = new FlowLayout();  
  132.   
  133.                                    netPanelLayout.setAlignOnBaseline(true);  
  134.   
  135.                                    netPanel.setBorder(BorderFactory.createTitledBorder(“选择网卡”));  
  136.   
  137.                                    netPanel.setAlignmentX(Component.LEFT_ALIGNMENT);  
  138.   
  139.                                    jPanel_west.add(netPanel);  
  140.   
  141.                                    netPanel.setLayout(netPanelLayout);  
  142.   
  143. //                                 netPanel.setPreferredSize(new java.awt.Dimension(239, 56));  
  144.   
  145.                                    {  
  146.   
  147.                                           devices = JpcapCaptor.getDeviceList();  
  148.   
  149.                                           if(devices == null){  
  150.   
  151.                                                  JOptionPane.showMessageDialog(frame, “没有找到网卡”);  
  152.   
  153.                                                  dispose();  
  154.   
  155.                                                  return;  
  156.   
  157.                                           }  
  158.   
  159.                                           else{  
  160.   
  161.                                                  String[] names = new String[devices.length];  
  162.   
  163.                                                  for(int i=0;i < names.length;i++){  
  164.   
  165.                                                         names[i] = (devices[i].description == null?devices[i].name:devices[i].description);  
  166.   
  167.                                                  }  
  168.   
  169.                                                  netJComboBox = new JComboBox(names);  
  170.   
  171.                                           }  
  172.   
  173.                                                  netPanel.add(netJComboBox);        
  174.   
  175.                                    }  
  176.   
  177.                             }  
  178.   
  179.                             {  
  180.   
  181.                                    CheckBox = new JCheckBox();  
  182.   
  183.                                    jPanel_west.add(CheckBox);  
  184.   
  185.                                    FlowLayout CheckBoxLayout = new FlowLayout();  
  186.   
  187.                                    CheckBoxLayout.setAlignOnBaseline(true);  
  188.   
  189.                                    CheckBox.setText(“\u662f\u5426\u8bbe\u7f6e\u4e3a\u6df7\u6742\u6a21\u5f0f”);  
  190.   
  191.                                    CheckBox.setLayout(null);  
  192.   
  193.                             }  
  194.   
  195.                             {  
  196.   
  197.                                    filterPanel = new JPanel();  
  198.   
  199.                                    filterPanel.setBorder(BorderFactory.createTitledBorder(“捕获过滤器”));  
  200.   
  201.                                    filterPanel.setAlignmentX(Component.LEFT_ALIGNMENT);  
  202.   
  203.                                    FlowLayout filterPanelLayout = new FlowLayout();  
  204.   
  205.                                    filterPanelLayout.setAlignment(FlowLayout.LEFT);  
  206.   
  207.                                    filterPanelLayout.setAlignOnBaseline(true);  
  208.   
  209.                                    jPanel_west.add(filterPanel);  
  210.   
  211.                                    filterPanel.setLayout(filterPanelLayout);  
  212.   
  213.                                    {  
  214.   
  215.                                           TextField = new JTextField(20);  
  216.   
  217.                                           filterPanel.add(TextField);  
  218.   
  219.                                    }  
  220.   
  221.                             }  
  222.   
  223.                      }  
  224.   
  225.                      {  
  226.   
  227.                             jPanel_east = new JPanel();  
  228.   
  229.                             jPanel_east.setLayout(new BoxLayout(jPanel_east,BoxLayout.Y_AXIS));  
  230.   
  231.                             getContentPane().add(jPanel_east);  
  232.   
  233.    
  234.   
  235.                             {  
  236.   
  237.                                    caplenPanel = new JPanel();  
  238.   
  239.                                    caplenPanel.setBorder(BorderFactory.createTitledBorder(“最长字长”));  
  240.   
  241.                                    caplenPanel.setAlignmentX(Component.LEFT_ALIGNMENT);  
  242.   
  243.                                    jPanel_east.add(caplenPanel);  
  244.   
  245.                                    caplenPanel.setLayout(new BoxLayout(caplenPanel,BoxLayout.Y_AXIS));  
  246.   
  247.    
  248.   
  249.                                    {  
  250.   
  251.                                           caplenTextField = new JTextField(20);  
  252.   
  253.                                           caplenPanel.add(caplenTextField);  
  254.   
  255.                                           caplenTextField.setText(“1514”);  
  256.   
  257.                                           caplenTextField.setEnabled(false);  
  258.   
  259.                                    }  
  260.   
  261.                                    {  
  262.   
  263.                                           wholeRadioButton = new JRadioButton();  
  264.   
  265.                                           FlowLayout userRadioButtonLayout = new FlowLayout();  
  266.   
  267.                                           userRadioButtonLayout.setAlignOnBaseline(true);  
  268.   
  269.                                           caplenPanel.add(wholeRadioButton);  
  270.   
  271.                                           wholeRadioButton.setText(“\u6574\u4e2a\u6570\u636e\u62a5”);  
  272.   
  273.                                           wholeRadioButton.setSelected(true);  
  274.   
  275.    
  276.   
  277.                                           wholeRadioButton.addActionListener(this);  
  278.   
  279.                                    }  
  280.   
  281.                                    {  
  282.   
  283.                                           headRadioButton = new JRadioButton();  
  284.   
  285.                                           caplenPanel.add(headRadioButton);  
  286.   
  287.                                           headRadioButton.setText(“\u4ec5\u9996\u90e8”);  
  288.   
  289.    
  290.   
  291.                                           headRadioButton.addActionListener(this);  
  292.   
  293.                                    }  
  294.   
  295.                                    {  
  296.   
  297.                                           userRadioButton = new JRadioButton();  
  298.   
  299.                                           caplenPanel.add(userRadioButton);  
  300.   
  301.                                           userRadioButton.setText(“\u5176\u4ed6\u90e8\u5206”);  
  302.   
  303.    
  304.   
  305.                                           userRadioButton.addActionListener(this);  
  306.   
  307.                                    }  
  308.   
  309.                                    ButtonGroup group=new ButtonGroup();  
  310.   
  311.                                    group.add(wholeRadioButton);  
  312.   
  313.                                    wholeRadioButton.setActionCommand(“Whole”);  
  314.   
  315.                                    group.add(headRadioButton);  
  316.   
  317.                                    headRadioButton.setActionCommand(“Head”);  
  318.   
  319.                                    group.add(userRadioButton);  
  320.   
  321.                                    userRadioButton.setActionCommand(“user”);  
  322.   
  323.                             }  
  324.   
  325.                             {  
  326.   
  327.                                    buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));  
  328.   
  329. //                                 buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS));  
  330.   
  331.                                    jPanel_east.add(buttonPanel);  
  332.   
  333.    
  334.   
  335.                                    {  
  336.   
  337.                                           okButton = new JButton();  
  338.   
  339.                                           buttonPanel.add(okButton);  
  340.   
  341.                                           FlowLayout cancelButtonLayout = new FlowLayout();  
  342.   
  343.                                           cancelButtonLayout.setAlignOnBaseline(true);  
  344.   
  345.                                           okButton.setText(“\u786e\u5b9a”);  
  346.   
  347.    
  348.   
  349.                                           okButton.setActionCommand(“ok”);  
  350.   
  351.                                           okButton.addActionListener(this);  
  352.   
  353.                                    }  
  354.   
  355.                                    {  
  356.   
  357.                                           cancelButton = new JButton();  
  358.   
  359.                                           buttonPanel.add(cancelButton);  
  360.   
  361.                                           cancelButton.setText(“\u53d6\u6d88”);  
  362.   
  363.    
  364.   
  365.                                           cancelButton.setActionCommand(“cancel”);  
  366.   
  367.                                           cancelButton.addActionListener(this);  
  368.   
  369.                                    }  
  370.   
  371. //                                 buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);  
  372.   
  373.                             }  
  374.   
  375.                      }  
  376.   
  377.                      getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.X_AXIS));  
  378.   
  379.    
  380.   
  381.                      getContentPane().add(jPanel_west);  
  382.   
  383.    
  384.   
  385.                      getContentPane().add(jPanel_east);  
  386.   
  387.    
  388.   
  389.                      pack();  
  390.   
  391.    
  392.   
  393.               } catch (Exception e) {  
  394.   
  395.                      e.printStackTrace();  
  396.   
  397.               }  
  398.   
  399.        }  
  400.   
  401.        public void actionPerformed(ActionEvent evt){  
  402.   
  403.               String cmd=evt.getActionCommand();  
  404.   
  405.                 
  406.   
  407.               if(cmd.equals(“Whole”)){  
  408.   
  409.                      caplenTextField.setText(“1514”);  
  410.   
  411.                      caplenTextField.setEnabled(false);  
  412.   
  413.               }else if(cmd.equals(“Head”)){  
  414.   
  415.                      caplenTextField.setText(“68”);  
  416.   
  417.                      caplenTextField.setEnabled(false);  
  418.   
  419.               }else if(cmd.equals(“user”)){  
  420.   
  421.                      caplenTextField.setText(“”);  
  422.   
  423.                      caplenTextField.setEnabled(true);  
  424.   
  425.                      caplenTextField.requestFocus();  
  426.   
  427.               }else if(cmd.equals(“ok”)){  
  428.   
  429.                      try{  
  430.   
  431.                             int caplen=Integer.parseInt(caplenTextField.getText());  
  432.   
  433.                             if(caplen<68 || caplen>1514){  
  434.   
  435.                                    JOptionPane.showMessageDialog(null,”捕获长度必须介于 68 和 1514之间”);  
  436.   
  437.                                    return;  
  438.   
  439.                             }  
  440.   
  441.                               
  442.   
  443.                             jpcap=JpcapCaptor.openDevice(devices[netJComboBox.getSelectedIndex()],caplen,  
  444.   
  445.                                           CheckBox.isSelected(),50);  
  446.   
  447.                               
  448.   
  449.                             if(TextField.getText()!=null && TextField.getText().length()>0){  
  450.   
  451.                                    jpcap.setFilter(TextField.getText(),true);  
  452.   
  453.                             }  
  454.   
  455.                      }catch(NumberFormatException e){  
  456.   
  457.                             JOptionPane.showMessageDialog(null,”捕获长度必须是正整数”);  
  458.   
  459.                      }catch(java.io.IOException e){  
  460.   
  461.                             JOptionPane.showMessageDialog(null,e.toString());  
  462.   
  463.                             jpcap=null;  
  464.   
  465.                      }finally{  
  466.   
  467.                             dispose();  
  468.   
  469.                      }  
  470.   
  471.                 
  472.   
  473.               }else if(cmd.equals(“cancel”)){  
  474.   
  475.                      dispose();  
  476.   
  477.               }  
  478.   
  479.        }  
  480.   
  481.          
  482.   
  483.        public static JpcapCaptor getJpcap(JFrame parent){  
  484.   
  485.               new Jcapturedialog(parent).setVisible(true);  
  486.   
  487.               return jpcap;  
  488.   
  489.        }  
  490.   
  491. }  

这一部分主要是界面操作,根据jigloo插件对不同的按钮和文本框还有其他的组件设置监听操作,以激发不同的函数操作,主要是devices = JpcapCaptor.getDeviceList();语句和

jpcap=JpcapCaptor.openDevice(devices[netJComboBox.getSelectedIndex()],caplen,

CheckBox.isSelected(),50);语句要选择一下监听的网卡,比如说笔记本就有两个网卡,一个无线一个有线,选择一下就会监听相应的网卡。函数

public static JpcapCaptor getJpcap(JFrame parent){

       new Jcapturedialog(parent).setVisible(true);

       return jpcap;

    }

返回jpcap,这个jpcap就是对应的选择上的网卡对象,接下来就从对应的网卡对象jpcap上不断得到数据包。

作者: 执着小钟

执着小钟

发表评论