style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-7505528228218001"
data-ad-slot="1225241371">

Step 2 - setup the main class

We are going to setup the main class as the packet handler as well. All it needs to do is implement the PcapPacketHandler interface. We are also going to use a static main method that will setup the libpcap portion and register our application as listener to libpcap packets.

public class IpReassemblyExample implements PcapPacketHandler {

  public static void main(String[] args) {
    StringBuilder errbuf = new StringBuilder();
    Pcap pcap = Pcap.openOffline("tests/test-ipreassembly2.pcap", errbuf);
    if (pcap == null) {
      System.err.println(errbuf.toString());
      return;
    }

    pcap.loop(
      6, // Collect 6 packets
      new IpReassemblyExample(
        5 * 1000, // 5 second timeout for reassembly buffer
        new IpReassemblyBufferHandler() {/*Omitted for now */}),
      "");
  }
}

Here we are using the static main method as a start for our application. We open up a capture file and enter a dispatch loop. We're only collecting 6 packets and we are registering out application as the PcapPacketHandler. Our constructor takes a IpReassemblyBufferHandler that will be notified with reassembled buffers. We create an anonymous class for that since we only do very minor work in its handler callback method.