1.用到的安装包有
·JpcapSetup-0.7.exe,双击安装;
·WinPcap4.0.exe,双击安装
·JpcapDumper-src.zip,解压;
 
2.要实现的功能
·与网卡绑定,并接收流经网卡的所有数据包;
·对数据包进行分析以获得源IP地址和目的IP地址;
·对数据包进行分析以获得传输层协议类型;
·对数据包进行分析以获得数据包的长度;
·对本机发送、接收的各运输层协议的数据流量进行统计,并显示统计结果;
 
3.实现类:
·TrafficStatictics.java
 
package com.socket.lzf;
import java.io.IOException;
import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;public class TrafficStatictics {
 private static TrafficStatictics ts;
 public static void main(String[] args) {
  // 启动统计线程
  TrafficStatictics.ins();
 } 
 public synchronized static TrafficStatictics ins() {
  if (null == ts) {
   TrafficStatictics ts = new TrafficStatictics();
   ts.init();
  }
  return ts;
 } 
 public void init() {  // 获取本机上的网络接口对象
  final NetworkInterface[] devices = JpcapCaptor.getDeviceList();  for (int i = 0; i < devices.length; i++) {
   NetworkInterface ni = devices[i];   // 大于零时为有效地址;不抓本机地址
   if (ni.addresses.length > 0) {    // 一个网卡可能有多个地址,只获取第一个
    String addr = ni.addresses[0].address.toString();    try {
     // 创建卡口上的抓取对象
     JpcapCaptor jpcap = JpcapCaptor.openDevice(ni, 2000, true,
       20);     // 创建对应的抓取线程并启动
     LoopPacketThread lpt = new LoopPacketThread(jpcap, addr);
     lpt.start();
     System.out.println(addr + "上的采集线程已启动!!!");    } catch (IOException e) {
     e.printStackTrace();
     System.out.println("抓取数据包时出现异常!!!");
    }   }
  } }
}
==============================================================================================·LoopPacketThread.java
 
package com.socket.lzf;
import jpcap.JpcapCaptor;
public class LoopPacketThread extends Thread {
 private JpcapCaptor jpcap = null;
 private String ipAddr = ""; public LoopPacketThread(JpcapCaptor jpcap, String ipAddr) {
  this.jpcap = jpcap;
  this.ipAddr = ipAddr;
 } // -1表示永久抓包
 public void run() {
  this.jpcap.loopPacket(-1, new ReceivePacket(this.ipAddr));
 }
}
============================================================================================== 
 
·ReceivePacket.java
 
package com.socket.lzf;
import java.text.SimpleDateFormat;
import java.util.Date;import jpcap.PacketReceiver;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;
import jpcap.packet.UDPPacket;public class ReceivePacket implements PacketReceiver {
 private String ipAddr = ""; private static int packetCount = 0;
 private static int tcpPacketCount = 0;
 private static int udpPacketCount = 0;
 private static int tcppPacketLength = 0;
 private static int udpPacketLength = 0; public ReceivePacket(String ipAddr) {
  this.ipAddr = ipAddr;
 } public void receivePacket(Packet packet) {
  this.packetCount++;  // 如果截获的包为TCP包
  if (packet instanceof jpcap.packet.TCPPacket) {
   this.tcpPacketCount++;   TCPPacket tcp = (TCPPacket) packet;
   this.tcppPacketLength += tcp.len;
   System.out
     .println("================================================================================");
   System.out.println("TCPPacket");
   System.out.println("|——*此数据包的源IP地址:" + tcp.src_ip);
   System.out.println("|——*此数据包的目的IP地址:" + tcp.dst_ip);
   System.out.println("|——*此数据包的传输层协议类型:" + tcp.protocol + "(TCP)");
   System.out.println("|——*此数据包的长度:" + tcp.len);  } else if (packet instanceof jpcap.packet.UDPPacket) {// 如果截获的包为UDP包
   this.udpPacketCount++;   UDPPacket udp = (UDPPacket) packet;
   this.udpPacketLength += udp.len;
   System.out
     .println("================================================================================");
   System.out.println("UDPPacket");
   System.out.println("|——*此数据包的源IP地址:" + udp.src_ip);
   System.out.println("|——*此数据包的目的IP地址:" + udp.dst_ip);
   System.out.println("|——*此数据包的传输层协议类型:" + udp.protocol + "(UDP)");
   System.out.println("|——*此数据包的长度:" + udp.len);  } else {
   // 如果截获的包非运输层协议包
   System.out.println("***捕获到非运输层协议的其他包!!!");
  }
  
  System.out.println();
  System.out.println("截止到目前:");
  System.out.println("|捕获到的运输层协议包的总数为:" + this.packetCount);
  System.out.println("|——*捕获到TCPPacket包的总数为:" + this.tcpPacketCount
    + ",总数据包长度为" + this.tcppPacketLength);
  System.out.println("|——*捕获到UDPPacket包的总数为:" + this.udpPacketCount
    + ",总数据包长度为" + this.udpPacketLength); }
 
 private static String currentTime(){
  Date date = new Date();
  
   SimpleDateFormat sdf=new SimpleDateFormat("yyyy/mm/dd hh:ff:mm");
  
   String strTime = sdf.format(date);
  
   return strTime;
  
 }}


==============================================================================================

 

4.参考资料:

JPCAP在线API参考文档:

http://netresearch.ics.uci.edu/kfujii/jpcap/doc/javadoc/index.html