Menu

Tutorial 1 Example 2

Bernardo Bulgarelli Labronici

Example 2

Difference Between Loop and Dispatch

        //Create a connection to pcap
        //For now let's ignore maxFrameLength, since it depends on the creation
        //of filter to work.
        Capture cap = new Capture(0);


        try {


            //Let's get all the devices available in the machine for pcap
            //If any error occurs it will be throw as an NetworkDeviceException
            List<NetworkDevice> networkDevices = cap.getNetworkDevices();

            //Assuming the first device is a valid device for capture.
            //we can use it.
            //Print basic info of device
            //We will connect to this device to sniff packets from
            NetworkDevice device = networkDevices.get(0);

            System.out.println();
            System.out.println("  Pcap Device Name: " + device.getName());
            System.out.println("Device Description: " + device.getDescription());

            //Print all address that this device can have
            //a device can have multiple address
            //(e.g. Ipv4 and Ipv6; Multihoming Ipv4; etc...)
            for(DeviceAddress deviceAddress :  device.getAddresses()){

                if(deviceAddress.getAddress() != null){
                    System.out.print("           Address: " + deviceAddress.getAddress().getHostAddress());

                    if(deviceAddress.getMask() != null)
                        System.out.print("/" + deviceAddress.getMask().getHostAddress());

                    if(deviceAddress.getBroadcast() != null)
                        System.out.print(" brdcast: " + deviceAddress.getBroadcast().getHostAddress());
                    System.out.println();
                }
            }


            System.out.println();
            //Add a new adapter to the Capture Listener.
            //Adapters can be of type CaptureAdapter for parsed packets, as the one in this example
            //or can be the type CaptureRawAdapter to receive packets as a ByteBuffer
            //This inner class should have a file of its own in real life projects
            class Adapter implements CaptureAdapter{

                //Count frames
                private int count = 0; 

                public void resetCount(){
                    count = 0;
                }

                @Override
                public void capture(Frame frame) {

                    //counting frames
                    count++;

                    //Count Frames
                    System.out.print(" " + count);

                }

            }

            Adapter adpater = new Adapter();

            cap.addListener(adpater);   


            //Connect to device (ignore timeout, show packets as they arrive)
            cap.connect(device, -1);



            System.out.println("Dispatch");
            //Start the Capture and block until the 1000 packet. 
            //Unless you are in a VERY have loaded network 1000 will not be captured
            //before the buffer is empty, so CaptureFramesDispatch will capture less packets
            int captured = cap.CaptureFramesDispatch(1000);
            System.out.println();
            System.out.println("Dispatch has captured " + captured + " frames");
            System.out.println();

            adpater.resetCount();

            //Start the Capture and block until the 20 
            //packet has been received and processed. No matter how heavy or light loaded 
            //is your network, all the 20 packets will always be capture
            System.out.println("Loop");
            cap.CaptureFramesLoop(20);
            System.out.println();
            //Close the Capture Device
            cap.close();

        }catch(NetworkDeviceException e){

            System.err.println("Error in the cap.getNetworkDevices();");
            e.printStackTrace();

        }catch(CaptureS4JException e){

            System.err.println("Error in the connection.");
            e.printStackTrace();

        }catch (JNIException e) {

            System.err.println("Error With the Dll");
            e.printStackTrace();
        } catch (NotSupportedDatalinkS4JException e) {

            System.err.println("Datalink not Supported, only Ethernet");
            e.printStackTrace();
        }

The main difference between CaptureFramesLoop and CaptureFramesDispatch is that, CaptureFramesLoop block the thread until all the packets has been capture (or until BreakLoop is called, see [Tutorial 2 Example 2].

CaptureFramesDispatch only blocks until all the packets has been capture, or the buffer of the device is empty (or until BreakLoop is called, see [Tutorial 2 Example 2])

The Advantage of dispatch mode over loop mode is that in low traffic networks there is no possibility that the program will hang for long periods. if the packets are not receive are the buffer empty the function will just return having received fewer packets.

The advantage of loop over dispatch is that, if you have an obligation to receive a certain amount of packets, it is simpler to call loop that guarantee that this packet will be received


Related

Wiki: Tutorial

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.