Jnetpcap Brief

Jnetpcap Brief

Recent projects need to do a local network traffic analysis, based on the Java language, check the internet a lot of information. Finally, Jnetpcap realized here to be a record.

 

Just to use my tools and versions:

     Eclipse               Version: 2019-06 (4.12.0)

     JDK                    Version:12.0.1,2019-04-16

     WinPcap            Version:4.1.3

     JnetPcap           Version:1.4.r1425

 

Java does not support the underlying network, you need to use third-party packages JNI wrapper different systems of C library to provide Java upper interface, a number of other libraries like JPcap are in disrepair, I am here is to use Jnetpcap achieve. Here are the steps, I need to use the file here attached link:

 

    WinPcap: https://pan.baidu.com/s/1Xu-HAaGSkOR-WriJM4wrBw , extraction code: yti0

    JnetPcap: https://pan.baidu.com/s/16bfUf8jHIH7osmC7rd0i-w       , extraction code: 6pcx

JnetPcap developer documentation: https://pan.baidu.com/s/1O1GDTpTHmtHxMZXo3YXMdQ , extraction code: mg2y

 

First, install WInPcap (Windows system)

Use JnetPcap to be installed in the system WinPcap, here is the Windows, Linux, need to install Lipcap, I'm here mainly to introduce Windows system.

 

WinPcap installation packages have been given above, is mounted directly on the lower line.

 

 

 

Second, the installation JnetPcap

As used herein Jnetpcap I import Jar package form, without the use of Maven, I always displayed after adding a dependency can not be downloaded in a central warehouse, I do not know what went wrong, then simply using means little trouble, the import Jar package, here step more trouble.

 

First linked above JnetPcap download and unzip, then perform the following two steps:

     (1), was added into the .dll file system path 

        It should be the jnetpcap.dll and  jnetpcap.dll  added to the system path, add here the path itself or under JDK / bin path into the system can be, here for convenience, just copy the two files to C: \ Windows \ System32 next on the list.

 

     (2) introducing Jar package in the project

       In Eclipse, click File -> New -> Folder, name fill lib, then copy the file above jnetpcap.jar unzip the file to the next file you just created folder.

       Then there is one step, right-click the project, click on the bottom of the Properties, select Java Build Path -> Libraries, pay attention to click Classpath, do not pipe above the Modulepath, if the above is added to the running time would have been reported java.lang .NoClassDefFoundError.

       Click on the right click Classpath Add JARS, just find the file in the lib folder inside the jnetpcap.jar click Add on the line, after the success should look like below.

 

 Such a configuration is basically the basic JnetPcap success.

 

 Third, simple to use

 Here I give a few examples of the use of JnetPcap.

 

Get Device card:

    //此方法用于获取设备上的网卡设施
    public static ArrayList<PcapIf> CaptureNet(){
        CaptureUtil.flag=false;
        
        // 用于存储搜索到的网卡
        ArrayList<PcapIf> alldevs = new ArrayList<PcapIf>(); 
        
        //取得设备列表
        int r = Pcap.findAllDevs(alldevs, errbuf);
        if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
            JOptionPane.showMessageDialog(null,errbuf.toString(),"错误",JOptionPane.ERROR_MESSAGE); 
            return null;
           }
        return alldevs;
    }

 

 

 

 打开选中网卡并捕获数据包:

        //打开选中的设备
        int snaplen = Pcap.DEFAULT_SNAPLEN; // 默认长度为65535
        int flags = Pcap.MODE_PROMISCUOUS;  // 混杂模式,捕获所有类型的包
        int timeout = 5 * 1000;             // 5 seconds in millis
        Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
        if (pcap == null) {
            JOptionPane.showMessageDialog(null,errbuf.toString(),"错误",JOptionPane.ERROR_MESSAGE);
            return;
        }

 

 

 

开始监听:

pcap.loop(int cnt, JPacketHandler<T> handler, T user)

 

 

 

当然上面只是一些代码示例,不是完整的代码,在之后的博客中会更新完整的代码。

 

上面给出了JnetPcap使用文档的下载链接,有兴趣的话最好还是照着使用文档来使用。

 

上面有什么错误还希望大家指正,希望能和大家多交流。

 

 

本文系原创,转载还请注明出处。

 

Guess you like

Origin www.cnblogs.com/hzauxx/p/11128747.html
Recommended