0

when I capture a specific packet I can't reproduce the same thing using the same ping. ( for example when you run the project and you go the cmd "ping somethingthatdoesntwork.comm" result : couldn't reach the page, the second time you do the same ping nothing happens.

public class Pcap4jLoop {

    public static void main(String[] args)
            throws PcapNativeException, IOException, NotOpenException, InterruptedException {
        String filter = null;
        if (args.length != 0) {
            filter = args[0];
        }

        PcapNetworkInterface nif = new NifSelector().selectNetworkInterface();
        if (nif == null) {
            System.exit(1);
        }
        final PcapHandle handle = nif.openLive(65536, PromiscuousMode.NONPROMISCUOUS, 10);

        PacketListener listener = new PacketListener() {
            public void gotPacket(PcapPacket packet) {
                printPacket(packet, handle);
            }

        };

        while (true) {
            handle.loop(1, listener);
            Thread.sleep(10);
        }
    }

    private static void printPacket(Packet packet, PcapHandle ph) {
        StringBuilder sb = new StringBuilder();
        if (packet != null)
            if (packet.toString().contains("Question") && packet.toString().contains("Authority")) {
                System.out.println(packet.toString().split("DNS Header")[1].split("QNAME: ")[1].split("QTYPE")[0]);
                if (packet.toString().contains("RCODE: 3")) {
                    System.out.println("Couldn't reach this page");
                }
                System.out.println("_______________________________________");
            }
    }
}
LMC
  • 10,453
  • 2
  • 27
  • 52
  • Perhaps packets arrive while your app sleeps :) – LMC Jan 30 '18 at 19:53
  • it's just for 10 ms and i'm not that fast ^^ – Nouâmane Raji Jan 30 '18 at 20:09
  • lol but 10 ms in terms of network speed is a lot. How many ping packets are you sending? You read 1 packet, that could take a few millis if not nanos and then sleep a lot. Try sending a lot of pings and see what happens. – LMC Jan 30 '18 at 20:12
  • i'm just checking if a certain page respond or not. i'm sending one ping every 10000 ms it works when i use different addresses but not the same one – Nouâmane Raji Jan 30 '18 at 20:23
  • please, add how are you sending the ping command. – LMC Jan 30 '18 at 20:31
  • for example open cmd and try to ping awrongadress.comx while running this and then it's gonna show Couldn't reach this page, then try to do it again and nothing's gonna happen – Nouâmane Raji Jan 30 '18 at 20:52
  • Seems the error is in count value, see [here](https://programtalk.com/vs/pcap4j/pcap4j-sample/src/main/java/org/pcap4j/sample/Loop.java/). Try 1000 or something. – LMC Jan 30 '18 at 21:42

0 Answers0