为什么一点菜单项startcatch我的窗体就会卡住,但是程序还在运行,求大神帮忙

这是使用jpcap进行抓包

下面是我的三个类,我的目的是要把抓到的包的信息添加到JTextArea文本框内,我也不知道是我的事件监听器写错了还是其他有问题,求大神帮看一下,找出问题在哪

package jpcap;
import java.io.IOException;

public class JpcapPacket {
    
    private static int caplen = 1512;
    private static boolean promiscCheck = true;
    static Reciver rec = new Reciver();
    
    public  void catchPacket() {
        
        NetworkInterface[] devices = JpcapCaptor.getDeviceList();
        
        /*for(NetworkInterface n : devices)
        {
            System.out.println(n.name + "     |     " + n.description);
        }
        System.out.println("-------------------------------------------");*/
        
        JpcapCaptor jpcap = null;
        try{
            jpcap = JpcapCaptor.openDevice(devices[4], caplen, promiscCheck, 50);
        }catch(IOException e)
        {
            e.printStackTrace();
        }
        
        while(true) {
            jpcap.processPacket(-1, rec);
        }
        
    }
    
}

————————————————————————————————————————————————————

 

package jpcap;

import jpcap.packet.*;


public class Reciver implements PacketReceiver {

    static int count = 0;
    
    public void receivePacket(Packet p) {
        
        if(p instanceof IPPacket && ((IPPacket)p).version == 4)
        {
            count++;
            IPPacket ip = (IPPacket)p;//强转
            System.out.println("运行");
            MainFrame.mf.addTextArea("第" + count + "个包:");
            MainFrame.mf.addTextArea("版本:IPv4");
            MainFrame.mf.addTextArea("优先权:" + ip.priority);
            MainFrame.mf.addTextArea("区分服务:最大的吞吐量" + ip.t_flag);
            MainFrame.mf.addTextArea("区分服务:最高的可靠性:" + ip.r_flag);
            MainFrame.mf.addTextArea("长度:" + ip.length);
            MainFrame.mf.addTextArea("标识:" + ip.ident);
            MainFrame.mf.addTextArea("DF:Don't Fragment: " + ip.dont_frag);
            MainFrame.mf.addTextArea("NF:Nore Fragment: " + ip.more_frag);
            MainFrame.mf.addTextArea("片偏移:" + ip.offset);
            MainFrame.mf.addTextArea("生存时间:"+ ip.hop_limit);
            
            String protocol ="";
            switch(new Integer(ip.protocol))
            {
            case 1:protocol = "ICMP";break;
            case 2:protocol = "IGMP";break;
            case 6:protocol = "TCP";break;
            case 8:protocol = "EGP";break;
            case 9:protocol = "IGP";break;
            case 17:protocol = "UDP";break;
            case 41:protocol = "IPv6";break;
            case 89:protocol = "OSPF";break;
            default : break;
            }
            MainFrame.mf.addTextArea("协议:" + protocol);
            MainFrame.mf.addTextArea("源IP " + ip.src_ip.getHostAddress());
            MainFrame.mf.addTextArea("目的IP " + ip.dst_ip.getHostAddress());
            MainFrame.mf.addTextArea("源主机名: " + ip.src_ip);
            MainFrame.mf.addTextArea("目的主机名: " + ip.dst_ip);
            MainFrame.mf.addTextArea("----------------------------------------------");
        }

    }

}

——————————————————————————————————————————————————————

package jpcap;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MainFrame extends JFrame {

    public static MainFrame mf = new MainFrame();
    private  boolean isstart = false;
    private  boolean isstop = true;
    private  JFrame jf;
    private  JPanel jp;
    private  JScrollPane jsp;
    private  JTextArea jta;
    private  JMenuBar jmb;
    private  JMenu start;
    private  JMenu stop;
    private  JMenuItem startcatch;
    private  JMenuItem stopcatch;
    
    public MainFrame() {
        
        jf = new JFrame("抓包工具");
        start = new JMenu("开始");
        stop = new JMenu("结束");
        jmb = new JMenuBar();
        startcatch = new JMenuItem("开始抓包");
        stopcatch = new JMenuItem("停止抓包");
        jp = new JPanel();
        jta = new JTextArea(30,60);
        jsp = new JScrollPane(jta);
    }
    
    public class StartListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if(!isstart && isstop) {
                
                clearTextArea();
                System.out.println("运行");
                new JpcapPacket().catchPacket();
                isstart = true;
                isstop = false;
            }
        }
        
    }
    
    public class StopListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if(isstart && !isstop) {
                
                isstart = false;
                isstop = true;
            }
        }
        
    }
    
    public void init() {
        
        jta.setEditable(false);
        start.add(startcatch);
        stop.add(stopcatch);
        jmb.add(start);
        jmb.add(stop);
        jf.setJMenuBar(jmb);
        startcatch.addActionListener(new StartListener());
        stopcatch.addActionListener(new StopListener());
        jp.add(jsp);
        jf.add(jp);
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jf.setResizable(false);
        jf.setLocation(400, 100);
        
    }
    
    public void showMe() {
        init();
        jf.pack();
        jf.setVisible(true);
    }
    
    public void addTextArea(String s) {
        mf.jta.append(s);
    }
    
    public void clearTextArea() {
        mf.jta.setText("");
    }
    public static void main(String[] args) {
        mf.showMe();
    }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值