Pcap
  • Introduction
  • Getting Started
    • Dependency management
    • Installing Libpcap or Npcap
    • Run as non root user
    • Logging
    • Obtaining the device list
    • Opening an adapter and capturing the packets
    • Capturing the packets without the callback
    • Capture statistics
    • Filtering the traffic
    • Write the packets to a capture file
    • Read packets from a capture file.
    • Sending packets
    • I/O Multiplexing
    • Restricted Method
  • Developer Guide
    • Branches to look
    • Build from Source
    • Notes
  • Packet Structure
    • Packet Header
    • Packet Buffer
  • Packet Codec
    • Using packet codec
    • Adding protocol support
  • Others
  • Thanks to
  • Fork me on Github
Powered by GitBook
On this page

Was this helpful?

  1. Getting Started

Capture statistics

Getting statistics about packets received and dropped in a live capture.

public static void main(String[] _args)
    throws ErrorException, PermissionDeniedException, PromiscuousModePermissionDeniedException,
        TimestampPrecisionNotSupportedException, RadioFrequencyModeNotSupportedException,
        NoSuchDeviceException, ActivatedException, InterfaceNotUpException,
        InterfaceNotSupportTimestampTypeException {
  var service = Service.Creator.create("PcapService");
  try (var pcap = service.live(service.interfaces(), new DefaultLiveOptions())) {
    PacketHeader header = pcap.allocate(PacketHeader.class);
    PacketBuffer buffer = pcap.allocate(PacketBuffer.class);
    for (int i = 0; i < 10; i++) {
      try {
        pcap.nextEx(header, buffer);
        System.out.println("Header   : " + header);
        System.out.println("Packet   : " + buffer);
        val stats = pcap.stats();
        System.out.println("Stats    : " + stats);
      } catch (BreakException e) {
        System.err.println(e.getMessage());
      } catch (TimeoutException e) {
        System.err.println(e.getMessage());
      }
    }
  }
}
fun main() {
  val service = Service.Creator.create("PcapService")
  val pcap = service.live(service.interfaces(), DefaultLiveOptions())
  pcap.setFilter("icmp", true)
  val header = pcap.allocate(PacketHeader::class.java)
  val buffer = pcap.allocate(PacketBuffer::class.java)
  for (i in 0..9) {
    try {
      pcap.nextEx(header, buffer)
      println("Header   : $header")
      println("Packet   : $buffer")
      val stats = pcap.stats()
      println("Stats    : $stats")
    } catch (e: BreakException) {
      System.err.println(e.message)
    } catch (e: TimeoutException) {
      System.err.println(e.message)
    }
  }
  pcap.close()
}
def main(_args: Array[String]): Unit = {
  val service = Service.Creator.create("PcapService")
  val pcap = service.live(service.interfaces(), new DefaultLiveOptions())
  val header = pcap.allocate(classOf[PacketHeader])
  val buffer = pcap.allocate(classOf[PacketBuffer])
  for (i <- 0 until 10) {
    try {
      pcap.nextEx(header, buffer)
      System.out.println("Header   : " + header)
      System.out.println("Packet   : " + buffer)
      val stats = pcap.stats()
      System.out.println("Stats    : " + stats)
    } catch {
      case e: BreakException =>
        System.err.println(e.getMessage)
      case e: TimeoutException =>
        System.err.println(e.getMessage)
      case e: ErrorException =>
        System.err.println(e.getMessage)
    }
  }
  pcap.close()
}
static void main(String[] usrArgs) {
  def service = Service.Creator.create("PcapService")
  def pcap = service.live(service.interfaces(), new DefaultLiveOptions())
  def header = pcap.allocate(PacketHeader.class)
  def buffer = pcap.allocate(PacketBuffer.class)
  try {
    pcap.nextEx(header, buffer)
    println("Header  : " + header)
    println("Packet  : " + buffer)
    Statistics stats = pcap.stats();
    println("Stats   : " + stats)
  } catch (BreakException e) {
    System.err.println(e.getMessage())
  } catch (TimeoutException e) {
    System.err.println(e.getMessage())
  } catch (ErrorException e) {
    System.err.println(e.getMessage())
  }
  pcap.close()
}
PreviousCapturing the packets without the callbackNextFiltering the traffic

Last updated 4 years ago

Was this helpful?