我使用以下代码从存储在pcap文件中的数据包中读取源和目标IP。public void nextPacket(PcapPacket数据包,字符串用户){
Ip4 ip = new Ip4();
Ethernet eth=new Ethernet();
String sIP;
String dIP;
int totalLength=0;
if (packet.hasHeader(ip) == false){
return;
}
totalLength = totalLength+ ip.getPayloadLength();
sIP = org.jnetpcap.packet.format.FormatUtils.ip(ip.source());
dIP = org.jnetpcap.packet.format.FormatUtils.ip(ip.destination());
System.out.println("SIP = "+sIP+" "+"destIP = "+dIP+" "+"Payload Length = "+ip.getPayloadLength());
System.out.println("Total Length = "+totalLength);
}
但它什么也没显示,有带头的数据包。请帮帮我。
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
final Ip4 ip = new Ip4();
public void nextPacket(PcapPacket packet, String user) {
if(packet.hasHeader(Ip4.ID)){
packet.getHeader(ip);
byte[] dIP = new byte[4], sIP = new byte[4];
dIP = packet.getHeader(ip).destination();
sIP = packet.getHeader(ip).source();
String sourceIP = FormatUtils.ip(sIP);
String destinationIP = FormatUtils.ip(dIP);
System.out.printf("tcp.ip_src=%s%n",sourceIP);
System.out.printf("tcp.ip_dest=%s%n",destinationIP);
}
}
};
什么意思?它只是因为数据包没有标题而返回,还是在你端的代码中打印null?我不得不用这些小信息猜测,要么数据包没有ip报头。