我在像ToyVpn这样的应用中捕获IPv4数据包为了确保正确处理读取数据包I was told以保存它并将我创建的响应保存到pcap文件并在WireShark中打开它。 / p>
至于写入文件,我在https://stackoverflow.com/a/19170377/1065835
上找到答案来自http://jnetpcap.com/node/69的两个示例都在这里抛出相同的NPE:
PcapDumper dumper = pcap.dumpOpen(ofile); // output file
我可以做我想做的事吗?
这是我的代码:
StringBuilder errbuf = new StringBuilder();
String fname = "test-afs.pcap";
new File(fname).createNewFile();
Pcap pcap = Pcap.openOffline(fname, errbuf);
String ofile = "tmp-capture-file.cap";
new File(ofile).createNewFile();
PcapDumper dumper = pcap.dumpOpen(ofile); // output file
pcap.loop(10, dumper); // Special native dumper call to loop
File file = new File(ofile);
System.out.printf("%s file has %d bytes in it!\n", ofile, file.length());
dumper.close(); // Won't be able to delete without explicit close
pcap.close();
答案 0 :(得分:1)
参数:
fname
- 指定要打开的文件的名称; jNetPcap不支持使用“ - ”作为字符串打开stdout的libpcap选项
您正在创建Pcap将在下一个LoC中打开的文件。我认为这不会起作用......:
String ofile = "tmp-capture-file.cap";
new File(ofile).createNewFile();
PcapDumper dumper = pcap.dumpOpen(ofile); // output file - BUT IT EXISTS!
在任何一种情况下,您都可以幸运地使用try
- catch
围绕该行,并查看Pcap.getErr()
条款中catch
是否有任何意义,但我投注已经存在的问题。
干杯,