我为一个可以测量计算机总数据使用量的程序编写了以下代码。我使用了 PcapPacket 类的 getTotalSize() 方法。它是直接表示数据使用情况(如果连续添加)还是我应该使用其他方法。
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();
}
}
以上是我编写的代码,试图计算通过网络的流量。它是否直接指示(如果不是完全准确)计算机的数据使用情况?