<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 156,  comments - 601,  trackbacks - 0
    在網絡上看到了這個項目,本人對這個不太了解,但挺興趣所以也推薦給大家,希望能一起學習。

    jNetPcap是libpcap的一個Java完整封裝。jNetPcap使 用與libpcap相同風格的API。libpcap是unix/linux平臺下的網絡數據包捕獲函數庫,大多數網絡監控軟件都以它為基礎。 Libpcap可以在絕大多數類unix平臺下工作。Libpcap提供了系統獨立的用戶級別網絡數據包捕獲接口,并充分考慮到應用程序的可移植性。

    jNetPcap 官方網站:http://jnetpcap.com/

    下面是官方上的一些演示示例:
    ClassicPcapExample.java

      1 /**
      2  * Copyright (C) 2008 Sly Technologies, Inc. This library is free software; you
      3  * can redistribute it and/or modify it under the terms of the GNU Lesser
      4  * General Public License as published by the Free Software Foundation; either
      5  * version 2.1 of the License, or (at your option) any later version. This
      6  * library is distributed in the hope that it will be useful, but WITHOUT ANY
      7  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      8  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
      9  * details. You should have received a copy of the GNU Lesser General Public
     10  * License along with this library; if not, write to the Free Software
     11  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     12  */
     13 package org.jnetpcap.examples;
     14 
     15 import java.nio.ByteBuffer;
     16 import java.util.ArrayList;
     17 import java.util.Date;
     18 import java.util.List;
     19 
     20 import org.jnetpcap.Pcap;
     21 import org.jnetpcap.PcapHandler;
     22 import org.jnetpcap.PcapIf;
     23 
     24 /**
     25  * This example is the classic libpcap example shown in nearly every tutorial on
     26  * libpcap. It gets a list of network devices, presents a simple ASCII based
     27  * menu and waits for user to select one of those interfaces. We will just
     28  * select the first interface in the list instead of taking input to shorten the
     29  * example. Then it opens that interface for live capture. Using a packet
     30  * handler it goes into a loop to catch a few packets, say 10. Prints some
     31  * simple info about the packets, and then closes the pcap handle and exits.
     32  * 
     33  * @author Mark Bednarczyk
     34  * @author Sly Technologies, Inc.
     35  */
     36 @SuppressWarnings("deprecation")
     37 public class ClassicPcapExample {
     38 
     39   public static void main(String[] args) {
     40         List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs
     41         StringBuilder errbuf = new StringBuilder();     // For any error msgs
     42         
     43         /********************************************
     44          * 取得設備列表
     45          ********************************************/
     46         int r = Pcap.findAllDevs(alldevs, errbuf);
     47         if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
     48             System.err.printf("Can't read list of devices, error is %s", errbuf
     49                 .toString());
     50             return;
     51         }
     52 
     53         System.out.println("Network devices found:");
     54 
     55         int i = 0;
     56         for (PcapIf device : alldevs) {
     57             System.out.printf("#%d: %s [%s]\n", i++, device.getName(), device
     58                 .getDescription());
     59         }
     60 
     61         PcapIf device = alldevs.get(2); // We know we have atleast 1 device
     62         System.out.printf("\nChoosing '%s' on your behalf:\n", device
     63             .getDescription());
     64     
     65         /***************************************
     66          * 打開選中的設備
     67          ***************************************/
     68         int snaplen = 64 * 1024;           // Capture all packets, no trucation
     69         int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
     70         int timeout = 10 * 1000;           // 10 seconds in millis
     71         Pcap pcap = Pcap
     72             .openLive(device.getName(), snaplen, flags, timeout, errbuf);
     73 
     74         if (pcap == null) {
     75             System.err.printf("Error while opening device for capture: "
     76                 + errbuf.toString());
     77             return;
     78         }
     79         
     80         /**********************************************************************
     81          * Third we create a packet hander which will be dispatched to from the
     82          * libpcap loop.
     83          **********************************************************************/
     84         PcapHandler<String> printSummaryHandler = new PcapHandler<String>() {
     85 
     86             public void nextPacket(String user, long seconds, int useconds,
     87                 int caplen, int len, ByteBuffer buffer) {
     88                 Date timestamp = new Date(seconds * 1000 + useconds/1000); // In millis
     89 
     90                 System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n",
     91                     timestamp.toString(), // timestamp to 1 ms accuracy
     92                     caplen, // Length actually captured
     93                     len,    // Original length of the packet
     94                     user    // User supplied object
     95                     );
     96             }
     97         };
     98 
     99         /************************************************************
    100          * Fourth we enter the loop and tell it to capture 10 packets
    101          ************************************************************/
    102         pcap.loop(10, printSummaryHandler, "jNetPcap rocks!");
    103 
    104         /*
    105          * Last thing to do is close the pcap handle
    106          */
    107         pcap.close();
    108     }
    109 }
    110 

    PcapDumperExample.java
    package org.jnetpcap.examples;

    import java.io.File;
    import java.nio.ByteBuffer;
    import java.util.ArrayList;
    import java.util.List;

    import org.jnetpcap.Pcap;
    import org.jnetpcap.PcapDumper;
    import org.jnetpcap.PcapHandler;
    import org.jnetpcap.PcapIf;


    public class PcapDumperExample {
      
    public static void main(String[] args) {
        List
    <PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs
        StringBuilder errbuf = new StringBuilder();     // For any error msgs

        
    /***************************************************************************
         * First get a list of devices on this system
         *************************************************************************
    */
        
    int r = Pcap.findAllDevs(alldevs, errbuf);
        
    if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
          System.err.printf(
    "Can't read list of devices, error is %s\n"
            errbuf.toString());
          
    return;
        }
        PcapIf device 
    = alldevs.get(1); // We know we have atleast 1 device

        
    /***************************************************************************
         * Second we 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 * 1000;           // 10 seconds in millis
        Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
        
    if (pcap == null) {
          System.err.printf(
    "Error while opening device for capture: %s\n"
            errbuf.toString());
          
    return;
        }
                    
        
    /***************************************************************************
         * Third we create a PcapDumper and associate it with the pcap capture
         **************************************************************************
    */
        String ofile 
    = "tmp-capture-file.cap";
        PcapDumper dumper 
    = pcap.dumpOpen(ofile); // output file

        
    /***************************************************************************
         * Fouth we create a packet handler which receives packets and tells the 
         * dumper to write those packets to its output file
         *************************************************************************
    */
        PcapHandler
    <PcapDumper> dumpHandler = new PcapHandler<PcapDumper>() {

          
    public void nextPacket(PcapDumper dumper, long seconds, int useconds,
            
    int caplen, int len, ByteBuffer buffer) {

            dumper.dump(seconds, useconds, caplen, len, buffer);
          }
        };

        
    /***************************************************************************
         * Fifth we enter the loop and tell it to capture 10 packets. We pass
         * in the dumper created in step 3
         *************************************************************************
    */
        pcap.loop(
    10, dumpHandler, 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(); 
    // Won't be able to delete without explicit close
        pcap.close();
                    
        
    if (file.exists()) {
          file.delete(); 
    // Cleanup
        }
      }
    }

    注:運行demo時,需要注意的情況:
    jNetPcap 類庫是都通JNI,調用系統的動態鏈接庫來實現與底層設備的交互。所以運行時需要加載。解決辦法如下:
    設置 -Djava.library.path參數
    java -Djava.library.path=c:\jnetpcap\lib -jar myJNetPcapApp.jar


    Good Luck!
    Yours Matthew!
    posted on 2008-11-27 22:55 x.matthew 閱讀(11800) 評論(1)  編輯  收藏 所屬分類: 最新開源動態
    主站蜘蛛池模板: 精品国产香蕉伊思人在线在线亚洲一区二区 | 免费的黄网站男人的天堂| 2020天堂在线亚洲精品专区| 亚洲酒色1314狠狠做| 亚洲精品美女在线观看| 亚洲熟妇av一区| 亚洲综合久久一本伊伊区| 伊人久久五月丁香综合中文亚洲| 亚洲一级特黄特黄的大片 | 狠狠亚洲婷婷综合色香五月排名| 亚洲免费无码在线| 伊人久久大香线蕉亚洲| 国产日韩亚洲大尺度高清| 亚洲av午夜成人片精品网站 | 亚洲毛片不卡av在线播放一区| 免费播放特黄特色毛片| 亚洲中久无码不卡永久在线观看| 中文字幕亚洲一区| 亚洲男人第一av网站| 亚洲综合久久成人69| 在线精品亚洲一区二区| 国产精品观看在线亚洲人成网| 又黄又大的激情视频在线观看免费视频社区在线 | 亚洲精品影院久久久久久| 久久精品国产亚洲av麻豆蜜芽| 亚洲熟妇无码一区二区三区| 国产天堂亚洲国产碰碰| 99精品视频在线观看免费| 在线日本高清免费不卡| 在线免费观看一级片| 亚洲av区一区二区三| 亚洲av无码成h人动漫无遮挡 | 99亚洲精品高清一二区| 亚洲中文精品久久久久久不卡| 黄色免费网址大全| 免费一级不卡毛片| 久久久久久99av无码免费网站| 免费在线视频一区| 亚洲人成在线电影| 亚洲精品无码高潮喷水A片软| xvideos永久免费入口|