P
P
Pcap
Search…
Capture statistics
Getting statistics about packets received and dropped in a live capture.
Java
Kotlin
Scala
Groovy
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()
}
Copy link