# Capturing the packets without the callback

Note: Both "PacketHeader" and "PacketBuffer" is not freed by the caller, and not guaranteed to be valid after the next call. If you needs it to remain valid, you must make a copy of it.

Docs[: libpcap](https://www.tcpdump.org/manpages/pcap.3pcap.html), [javadoc](https://javadoc.io/doc/com.ardikars.pcap/pcap-spi/latest/pcap/spi/Pcap.html).

{% tabs %}
{% tab title="Java" %}

```
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);
      } catch (BreakException e) {
        System.err.println(e.getMessage());
      } catch (TimeoutException e) {
        System.err.println(e.getMessage());
      }
    }
  }
}
```

{% endtab %}

{% tab title="Kotlin" %}

```
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")
    } catch (e: BreakException) {
      System.err.println(e.message)
    } catch (e: TimeoutException) {
      System.err.println(e.message)
    }
  }
  pcap.close()
}
```

{% endtab %}

{% tab title="Scala" %}

```
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)
    } 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()
}
```

{% endtab %}

{% tab title="Clojure" %}

```
(defn -main
  [& args]
  (def service (. Service$Creator (create "PcapService")))
  (def pcap (.live service (.interfaces service) (DefaultLiveOptions.)))
  (def header (.allocate pcap pcap.spi.PacketHeader))
  (def buffer (.allocate pcap pcap.spi.PacketBuffer))
  (dotimes [n 10]
    (.nextEx pcap header buffer)
    (println header)
    (println buffer))
  (.close pcap))
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pcap.ardikars.com/getting-started/capturing-the-packets-without-the-callback.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
