jnetpcap尝试从另一个程序运行捕获

时间:2015-12-04 14:07:44

标签: java multithreading swing swingworker jnetpcap

所以我有两个运行良好的项目。但是当我尝试将它们组合在一起时,我立刻就会在底部出现错误。

单独的文件(前两个属于一起): File1:

package pkg2buttongui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author root
 */
public class TestJavaHttpsDecrypt{

    private JFrame frame;
    private JTextField input;
    private JButton startButton;
    private JButton stopButton;

    public void go(){
        final CaptureNetTraffic cnt = new CaptureNetTraffic();
        frame = new JFrame("CaptureControl");
        frame.setLayout(new GridLayout(3,1));

        input = new JTextField("enter NIC");
        startButton = new JButton("start");
        stopButton = new JButton("stop");

        startButton.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    cnt.run(input.getText());
                }
            }
        );
        stopButton.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    cnt.stopCap();
                }
            }
        );

        frame.add(input);
        frame.add(startButton);
        frame.add(stopButton);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(800,500,100,150);
        frame.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new TestJavaHttpsDecrypt().go();
    }

}

文件2:

package pkg2buttongui;

import javax.swing.SwingWorker;

/**
 *
 * @author root
 */
public class CaptureNetTraffic extends Thread{

    int i = 0;

    private String text = null;
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            while(true){
                System.out.println(text);
                Thread.sleep(1);
                i++;
            }
        }
    };

    public void stopCap(){
        //Thread.interrupted();
        worker.cancel(true);
        System.out.println("stopping capture at " + i);
    }

    public void run(final String var){
        text = var;
        worker.execute();
    }
}

第二个项目:

    import java.io.File;  
    import java.io.FileNotFoundException;
    import org.jnetpcap.Pcap;  
    import org.jnetpcap.PcapDumper;  


    public class PcapDumperExample {  
      public static void main(String[] args) throws FileNotFoundException {  
        StringBuilder errbuf = new StringBuilder();     // For any error msgs  

        /*************************************************************************** 
         * open up the selected device 
         **************************************************************************/  
        int snaplen = 64 * 1024;           // Capture all packets, no trucation  
        int flags = Pcap.MODE_PROMISCUOUS; // capture all packets  
        int timeout = 10;           // 10 seconds in millis  
        Pcap pcap = Pcap.openLive("eth1", snaplen, flags, timeout, errbuf);  
        if (pcap == null) {  
          System.err.printf("Error while opening device for capture: %s\n",   
            errbuf.toString());  
          return;  
        }  

        /*************************************************************************** 
         * create a PcapDumper and associate it with the pcap capture 
         ***************************************************************************/  
        String ofile = "mycap.cap";  
        PcapDumper dumper = pcap.dumpOpen(ofile); // output file  

        /*************************************************************************** 
         * enter the loop and tell it to capture 10 packets. We pass 
         * in the dumper created in step 3 
         **************************************************************************/  
        pcap.loop(100, dumper);  

        File file = new File(ofile);  
        System.out.printf("%s file has %d bytes in it!\n", ofile, file.length());


        /*************************************************************************** 
         * Last thing to do is close the dumper and pcap handles 
         **************************************************************************/  
        dumper.close(); 
        pcap.close();  
        }  
      }  
    }  

现在我在这里尝试将它们放在一起,这样我就可以控制捕获的持续时间。为此,我将loop.infinite放在pcap.loop而不是100.并将使用以下文件与file1:

package testjavahttpsdecrypt;

import java.io.File;
import javax.swing.SwingWorker;

import static org.jnetpcap.Pcap.LOOP_INFINITE;

import org.jnetpcap.Pcap;  
import org.jnetpcap.PcapDumper;

/**
 *
 * @author root
 */
public class CaptureNetTraffic extends Thread {

    int i = 0;    
    private String text = null;
    Pcap pcap = new Pcap();
    PcapDumper dumper = new PcapDumper();
    String ofile = "meins.cap";  
    StringBuilder errbuf = new StringBuilder();
    int snaplen = 64 * 1024;           // Capture all packets, no trucation  
    int flags = Pcap.MODE_PROMISCUOUS; // capture all packets  
    int timeout = 10000;           // 10 seconds in millis  


    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            while(true){
                pcap = Pcap.openLive(text, snaplen, flags, timeout, errbuf);  
                if (pcap == null) {  
                  System.err.printf("Error while opening device for capture: %s\n",   
                    errbuf.toString());  
                  return null;  
                }
                dumper = pcap.dumpOpen(ofile);

                pcap.loop(100, dumper);                  
            }
        }
    };

    public void stopCap(){
        File file = new File(ofile);  
        System.out.printf("%s file has %d bytes in it!\n", ofile, file.length());
        dumper.close();
        pcap.close();
        //Thread.interrupted();
        worker.cancel(true);
        System.out.println("stopping capture at " + i);
    }

    public void run(final String var){
        text = var;
        worker.execute();
    }
}

现在当我运行file1时,我收到此错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: 
com.slytechs.library.NativeLibrary.dlopen(Ljava/lang/String;)J
    at com.slytechs.library.NativeLibrary.dlopen(Native Method)
    at com.slytechs.library.NativeLibrary.<init>(Unknown Source)
    at com.slytechs.library.JNILibrary.<init>(Unknown Source)
    at com.slytechs.library.JNILibrary.loadLibrary(Unknown Source)
    at com.slytechs.library.JNILibrary.register(Unknown Source)
    at com.slytechs.library.JNILibrary.register(Unknown Source)
    at com.slytechs.library.JNILibrary.register(Unknown Source)
    at org.jnetpcap.Pcap.<clinit>(Unknown Source)
    at testjavahttpsdecrypt.CaptureNetTraffic.<init>(CaptureNetTraffic.java:24)
    at testjavahttpsdecrypt.TestJavaHttpsDecrypt.go(TestJavaHttpsDecrypt.java:23)
    at testjavahttpsdecrypt.TestJavaHttpsDecrypt.main(TestJavaHttpsDecrypt.java:62)

由于第二个项目本身运行正常,我相信我已经正确设置了库(这是来自stackoverflow的一个帖子的提示) 我很欣赏这件事的每一个暗示/帮助。

0 个答案:

没有答案