<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)  編輯  收藏 所屬分類: 最新開源動態
    主站蜘蛛池模板: 91网站免费观看| 国产91色综合久久免费分享| 国产国产成年年人免费看片| 一本色道久久88—综合亚洲精品 | 日韩黄色免费观看| 久久精品亚洲视频| 国产在线精品免费aaa片| 亚洲AV无码一区二区乱孑伦AS| 亚洲一级毛片视频| h视频在线观看免费完整版| 亚洲成aⅴ人片在线影院八| 57pao国产成视频免费播放| 亚洲美女激情视频| 一二三四免费观看在线电影 | 人人玩人人添人人澡免费| 亚洲韩国精品无码一区二区三区| 亚洲一区二区三区四区视频| 黄网站色在线视频免费观看| 国产成人精品亚洲2020| 最新69国产成人精品免费视频动漫 | 曰韩亚洲av人人夜夜澡人人爽| 亚洲人成电影青青在线播放| 成年女人午夜毛片免费视频 | 亚洲精品无码专区2| 一级做a爰片性色毛片免费网站| 免费AA片少妇人AA片直播| 国产精品亚洲四区在线观看| 四虎精品亚洲一区二区三区| 国内精品久久久久影院免费| 亚洲AV色吊丝无码| 亚洲伦乱亚洲h视频| 2015日韩永久免费视频播放| 国产成人不卡亚洲精品91| 亚洲精品无码成人片久久| 久草视频在线免费| 男女猛烈无遮掩视频免费软件| 男男AV纯肉无码免费播放无码| 久久精品国产亚洲香蕉| 成年轻人网站色免费看| 国产又黄又爽胸又大免费视频| 国产美女被遭强高潮免费网站|