jnetpcap java 网络抓包
我想实现的功能:
1.开启进程进行网络抓包
2.运行一段程序,截取这段程序产生的数据包
3.停止抓包进程,保存数据包到本地
但我看到jnetpcap上的示例代码只能抓去特定个数的数据包,离我想要的效果有点差距,可能是我自己没有把
文档看透,哪位写过类似代码的请指点一下,谢谢!
jnetpcap网站:http://www.jnetpcap.com
抓取10个数据包的代码行:
pcap.loop(10, dumpHandler, dumper);
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 %sn", 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: %sn", 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 } } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://www.oschina.net/code/snippet_214658_44034