简体   繁体   English

使用jnetpcap进行数据使用计数

[英]Data usage counting using jnetpcap

I have written the following code for a program which could measure the total data usage of the computer. 我为程序编写了以下代码,该程序可以测量计算机的总数据使用量。 I have used the method getTotalSize() of the class PcapPacket. 我使用了PcapPacket类的getTotalSize()方法。 Is it directly indicative of the data usage (if added continuously) or should I use some other method. 是直接指示数据使用情况(如果连续添加)还是应该使用其他方法?

import org.jnetpcap.packet.PcapPacket; 
import org.jnetpcap.protocol.JProtocol; 

public class CapturePacket{

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


    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", errbuf
                .toString());
        return;
    }
    PcapIf device = (PcapIf) alldevs.get(0); // We know we have at least 1 device


    String ad = device.getHardwareAddress().toString();
    System.out.println("\nCurrently open adapter MAC:" + ad);

    int snaplen = 64 * 1024; // Capture all packets, no truncation
    int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
    int timeout = 10; //10*1000; // No timeout, non-interactive traffic
    final Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout,
            errbuf);
    if (pcap == null) {
        System.err.printf("Error while opening device for capture: "
                + errbuf.toString());
        return;
    }


    PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() 
     {
         long total_traffic = 0,count = 0;
         int i;


     public void nextPacket(PcapPacket packet, String user) {

         count += packet.getTotalSize();
         if( count>1048576 )
            {
                i++;
                total_traffic += count;
             System.out.println(i+"MB"+"\t total:"+total_traffic);
             count=count-1048576;

            }
     };
     };           
            pcap.loop(-1,jpacketHandler," ");  
            pcap.close(); 
            }

}

The above is a code I've written in an attempt to count the traffic passing through the network. 上面是我编写的代码,旨在计算通过网络的流量。 Is it directly indicative (if not perfectly accurate) of the data usage of the computer? 它是否直接指示(如果不是十分准确)计算机数据的使用情况?

You should use packet.getPacketWirelen() According to the documentation, it gets the original length as seen on the wire, even if the captured packet is truncated. 您应该使用packet.getPacketWirelen()根据文档,即使捕获的数据包被截断,它也将获得在导线上看到的原始长度。

Additionally you should check if the packet is from or to your internal network only, so then it shouldn't count towards your data usage(I'm assuming you want data usage as seen by your ISP). 另外,您应该检查数据包是仅来自内部网络还是来自内部网络,因此它不应该计入您的数据使用量(我假设您想要的是ISP所看到的数据使用量)。

After that it should be indicative of your data usage. 之后,它应该指示您的数据使用情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM