gpt4 book ai didi

java - 将表示 IP 数据包的字节数组写入 pcap 文件

转载 作者:行者123 更新时间:2023-11-30 08:11:46 26 4
gpt4 key购买 nike

我在 ToyVpn 这样的应用程序中捕获 IPv4 数据包确保我正确处理读取的数据包I was told将其和我创建的响应保存到 pcap 文件并在 WireShark 中打开它。

我使用jnetpcap-1.3.0-1.win64

关于写入文件,我在 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();

最佳答案

JavaDoc for Pcap.dumpOpen()

Parameters: fname - specifies the name of the file to open; currently the libpcap option to open stdout by using "-" as a string, is not supported by jNetPcap

您正在创建 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 子句中有意义,但我打赌已经存在的问题。

干杯,

关于java - 将表示 IP 数据包的字节数组写入 pcap 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30302210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com