ICMPdump.java
上传用户:luaite
上传日期:2022-08-06
资源大小:1k
文件大小:2k
源码类别:

网络编程

开发平台:

Java

  1. import jpcap.*;
  2. import java.net.*;
  3. public class ICMPdump implements JpcapHandler
  4. {
  5.    public void handlePacket(Packet packet){
  6.      System.out.println(packet);
  7.       if (packet instanceof ICMPPacket) {
  8.         System.out.println("协议类型:icmpPacket");
  9.       ICMPPacket icmpPacket = (ICMPPacket) packet;
  10.        /*System.out.println("Id=" + icmpPacket.id +":" );
  11.                     System.out.println("Type=" + icmpPacket.type);
  12.                     System.out.println("Code=" + icmpPacket.code);
  13.                     System.out.println("data=" + icmpPacket.data);*/
  14.        String srchost=String.valueOf(icmpPacket.getSourceAddress());
  15.        String dstHost = String.valueOf(icmpPacket.getDestinationAddress());
  16.       // System.out.println("icmpPacket sourceaddress"+icmpPacket.sourceaddress);
  17.      //  System.out.println("icmpPacket destinationaddress"+icmpPacket.destinationaddress);
  18.        System.out.println("icmpPacket type" + icmpPacket.type);
  19.        System.out.println("icmpPacket code" + icmpPacket.code);
  20.        System.out.println("icmpPacket checksum" + icmpPacket.checksum);
  21.        System.out.println("");
  22.        System.out.println("ip header" + icmpPacket.header);
  23.        System.out.println("icmpPacket data" + icmpPacket.data);
  24.      }
  25.    }
  26.    public static void main(String[] args) throws java.io.IOException{
  27.   String[] devices = Jpcap.getDeviceList();
  28.   System.out.println("Start capturing on "+devices[0]);
  29.   String deviceName = devices[0];
  30.   Jpcap jpcap = Jpcap.openDevice(deviceName, 1000, false, 20);
  31.   jpcap.loopPacket(-1, new ICMPdump());
  32.    }
  33. }