我怎样才能在Java中嗅探网络流量? [英] How could I sniff network traffic in Java?
问题描述
我只是四处寻找如何制作一个能用Java嗅探网络流量的程序,但我找不到任何东西。我想知道是否有任何方法可以查看网络流量。我听说过 Socket 的想法,但我不明白这会怎么样。所以无论如何,只是寻找API或自己编写的方式。
I was just looking around to find out how to make a program that would sniff my network traffic in Java, but I couldn't find anything. I wanted to know if there was any way to view the network traffic going by. I heard of an idea with a Socket, but I don't get how that would work. So anyways, just looking for an API or a way to write it myself.
编辑:
我很乐意喜欢API ,但我还想澄清一下使用Socket嗅探流量的方法。
I would gladly like an API, but I would also like clarification on the way to sniff traffic with a Socket.
推荐答案
jpcap , jNetPcap - 这些是Java中的pcap包装项目。
jpcap, jNetPcap -- those are pcap wrapper projects in Java.
Kraken - 类似项目,记录详尽有很多例子。
Kraken -- similar project, well documented with lots of examples.
来自Kraken网站的简单示例:
simple example from the Kraken web site:
public static void main(String[] args) throws Exception {
File f = new File("sample.pcap");
EthernetDecoder eth = new EthernetDecoder();
IpDecoder ip = new IpDecoder();
TcpDecoder tcp = new TcpDecoder(new TcpPortProtocolMapper());
UdpDecoder udp = new UdpDecoder(new UdpPortProtocolMapper());
eth.register(EthernetType.IPV4, ip);
ip.register(InternetProtocol.TCP, tcp);
ip.register(InternetProtocol.UDP, udp);
PcapInputStream is = new PcapFileInputStream(f);
while (true) {
// getPacket() will throws EOFException and you should call is.close()
PcapPacket packet = is.getPacket();
eth.decode(packet);
}
}
这篇关于我怎样才能在Java中嗅探网络流量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!