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

网络编程

开发平台:

Java

  1. package bysj;
  2. import jpcap.*;
  3. import java.net.*;
  4. public class ARPdump implements JpcapHandler
  5. {
  6.     public void handlePacket(Packet packet){
  7.       Packet p = packet;
  8.       try {
  9.         if (p instanceof ARPPacket) {
  10.           ARPPacket arpp = (ARPPacket) p;
  11.           System.out.println(arpp.hardtype);
  12.           System.out.println(arpp.prototype);
  13.           System.out.println(arpp.hlen);
  14.           System.out.println(arpp.plen);
  15.           System.out.println(arpp.operation);
  16.           System.out.println("ARP packet From:" + arpp.getSenderHardwareAddress());
  17.           System.out.println("ARP packet To:" + arpp.getTargetHardwareAddress());
  18.           System.out.println("From:" + arpp.getSenderProtocolAddress());
  19.           System.out.println("To:" + arpp.getTargetProtocolAddress());
  20.           //System.out.println(p.toString());
  21.         }
  22.       }
  23.       catch (Exception e) {
  24.         e.printStackTrace();
  25.       }
  26.     }
  27.       public static void main(String[] args) throws java.io.IOException {
  28.         String[] devices = Jpcap.getDeviceList();
  29.         System.out.println("Start capturing on " + devices[0]);
  30.         String deviceName = devices[0];
  31.         Jpcap jpcap = Jpcap.openDevice(deviceName, 1000, false, 20);
  32.         jpcap.loopPacket( -1, new ARPdump());
  33.       }
  34.   }