Hi! I'm confused with this code. I have a problem with stopping the thread. Can anybody help me?
Thank you! Hope for your help guys.
Code:
import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; import java.awt.*; import java.awt.event.*; import jpcap.JpcapCaptor; import jpcap.NetworkInterface; import jpcap.PacketReceiver; import jpcap.packet.*; public class MainGUI extends JFrame{ private JLabel lblintro; public JTextArea txtPackets; public JTextArea txtWarnings; private JPanel panel1; private JPanel panel2; private JPanel panel3; private JPanel panel4; private JPanel panel5; private JButton btnStart; private JButton btnStop; private JButton btnExit; private JScrollPane sbrText; public JpcapCaptor jpcap; public TestPacketReceiver TPR; protected JTabbedPane tabPane; public MainGUI(){ super("Packet Sniffer"); tabPane = new JTabbedPane(); panel1 = new JPanel(); panel3 = new JPanel(); tabPane.addTab("Main menu", null,panel1,"First panel"); lblintro = new JLabel ("Packet Sniffer"); panel1.setLayout(new BorderLayout()); panel1.add(panel3,BorderLayout.NORTH); panel3.setLayout(new FlowLayout()); panel3.add(lblintro); btnStart = new JButton ("Start"); btnStart.addActionListener(new ButtonListener()); btnStop = new JButton ("Stop"); btnStop.addActionListener(new ButtonListener()); btnExit = new JButton ("Exit"); btnExit.addActionListener(new ButtonListener()); panel3.add(btnStart); panel3.add(btnStop); panel3.add(btnExit); txtPackets = new JTextArea (); txtPackets.setLineWrap(true); txtPackets.setBorder(BorderFactory.createTitledBorder("Packets")); panel1.add(txtPackets,BorderLayout.CENTER); panel5 = new JPanel(); panel1.add(panel5,BorderLayout.SOUTH); txtWarnings = new JTextArea (); txtWarnings.setLineWrap(true); txtWarnings.setBorder(BorderFactory.createTitledBorder("Warnings")); panel1.add(txtWarnings,BorderLayout.SOUTH); panel2 = new JPanel(); tabPane.addTab("Logs", null, panel2, "Second panel"); panel4 = new JPanel(); tabPane.addTab("Send alerts",null,panel4); add(tabPane); } java.lang.Runnable rnner=new Runnable(){ public void run(){ jpcap.loopPacket(-1, new TestPacketReceiver()); } }; Thread t; public void startCapThread(final JpcapCaptor jpcap){ t = new Thread(rnner); t.start(); } //////////////////// This part below is not working //////////////////////// public void stopthread (final JpcapCaptor jpcap) { t.stop(); } public class ButtonListener implements ActionListener { @Override public void actionPerformed (ActionEvent e){ if (e.getSource()==btnStart) { //JOptionPane.showMessageDialog(null, "Packet sniffer started"); try{ final NetworkInterface[] devices = JpcapCaptor.getDeviceList(); for(int i=0;i<devices.length;i++){ NetworkInterface nc=devices[i]; jpcap = JpcapCaptor.openDevice(nc, 2000, true, 20); startCapThread(jpcap); System.out.println("开始抓取第"+i+"个卡口上的数据"); } }catch(Exception ef){ ef.printStackTrace(); System.out.println("启动失败: "+ef); } } if (e.getSource()==btnStop) { stopthread(jpcap); // JOptionPane.showMessageDialog(null, "Packet sniffer stopped"); System.out.println("STOPPED"); } if (e.getSource()==btnExit) { System.exit(0); } } } }
class TestPacketReceiver implements PacketReceiver { public String s; public void receivePacket(Packet packet) { //Tcp, java Socket if(packet instanceof jpcap.packet.TCPPacket){ TCPPacket p=(TCPPacket)packet; s="TCPPacket:| dst_ip "+p.dst_ip+":"+p.dst_port +"|src_ip "+p.src_ip+":"+p.src_port +" |len: "+p.len+" |flag: "+p.d_flag+" |ack_num: "+p.ack; System.out.println(s); } //UDP, tcp+udp else if(packet instanceof jpcap.packet.UDPPacket){ UDPPacket p=(UDPPacket)packet; s="UDPPacket:| dst_ip "+p.dst_ip+":"+p.dst_port +"|src_ip "+p.src_ip+":"+p.src_port +" |len: "+p.len+" |THIS: "+p.d_flag; System.out.println(s); } //ping, ICMPPacket else if(packet instanceof jpcap.packet.ICMPPacket){ ICMPPacket p=(ICMPPacket)packet; //ICMP String router_ip=""; for(int i=0;i<p.router_ip.length;i++){ router_ip+=" "+p.router_ip[i].getHostAddress(); } s="@ @ @ ICMPPacket:| router_ip "+router_ip +" |redir_ip: "+p.redir_ip +" |mtu: "+p.mtu +" |length: "+p.len+ " |This ICMP: "+p.addr_num; System.out.println(s); } else if(packet instanceof jpcap.packet.ARPPacket){ ARPPacket p=(ARPPacket)packet; //Returns the hardware address (MAC address) of the sender Object saa= p.getSenderHardwareAddress(); Object taa=p.getTargetHardwareAddress(); s="* * * ARPPacket:| SenderHardwareAddress "+saa +"|TargetHardwareAddress "+taa +" |len: "+p.len; System.out.println(s); } DatalinkPacket datalink =packet.datalink; if(datalink instanceof jpcap.packet.EthernetPacket){ EthernetPacket ep=(EthernetPacket)datalink; s=" datalink layer packet: " +"|DestinationAddress: "+ep.getDestinationAddress() +"|SourceAddress: "+ep.getSourceAddress(); System.out.println(s); } } }