kaitoy / pcap4j

A Java library for capturing, crafting, and sending packets.

Home Page:https://www.pcap4j.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Having trouble retrieving package data

ogozman opened this issue · comments

Hello,
I am trying to capture and parse packets, for this I need to retrieve the metadata from them, however, no matter what layer types i put into get() function, i always get null.

Here is a snippet of my code:

    while (handle.isOpen()) {
        Packet packet;
        try {
            packet = handle.getNextPacketEx();
            if (packet != null) {

                UdpPacket udpPacket = packet.get(UdpPacket.class);
                System.out.println(udpPacket);
            }
        } catch (EOFException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

Am I doing something wrong?

Same issue

Hey, I have forgotten to close the issue and write what helped me. Essentially, i have not understood at first that i need to add packet factories to class path separaterly. So the solution for me was the same as here
#244

I my case I used gradle and all needed to do was to add packetfactories package to gradle dependencies. Hope this helps