0

I'm quite new to JNetPcap and I'm still finding my way around with it, I'm trying to build a Packet sniffer for my project, Lately I'm trying printout packet information into a JTextArea by appending the information from a pcap.loop() that I am using, but when I set the first parameter using a specific integer value let say 5 the pcap.loop() outputs 5 packets that had been captured, Now what I want is to continuously capture and output the packet until I press the button stop. The syntax below shows the Packet handler.

PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {  

  public void nextPacket(PcapPacket packet, String user) {  

   // System.out.printf is included to check if my code works in a non GUI fashion
   System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n",  
                     new Date(packet.getCaptureHeader().timestampInMillis()),   
                     packet.getCaptureHeader().caplen(),  // Length actually captured  
                     packet.getCaptureHeader().wirelen(), // Original length   
                     user                                 // User supplied object  
                      );
                     Date a = new Date(packet.getCaptureHeader().timestampInMillis());
                      int b = packet.getCaptureHeader().caplen();
                      int c = packet.getCaptureHeader().wirelen();
                      String d = user;

                      pcktTextArea.append("Received packet at " + a + " caplen=" + Integer.toString(b) + " len=" + Integer.toString(b) + user + "\n" );
                      pcktTextArea.setForeground(Color.red);
                      pcktTextArea.setFont(font);
                    }  
                };  

Now this bit here is my pcktTextArea which I use append to print out the information in the textarea:

   pcktTextArea.append("Received packet at " + a + " caplen=" + Integer.toString(b) + " len=" + Integer.toString(b) + user + "\n" );
   pcktTextArea.setForeground(Color.red);
   pcktTextArea.setFont(font);

And Finally the Pcap.loop which I am having trouble with, if I replace that i with let say 5 it does get printed in the JTextArea but when I put the Pcap.LOOP_INFINTE it only prints the information through console but not in GUI JTextArea:

   int i = Pcap.LOOP_INFINITE;

    pcap.loop(i , jpacketHandler, " ");  

    /*************************************************************************** 
     * Last thing to do is close the pcap handle 
     **************************************************************************/  
    pcap.close();  

Is it because it has to finish the loop before printing the information out in the Textarea?

Rojee
  • 43
  • 8
  • I need your help guys any answers will do. – Rojee Feb 21 '13 at 11:14
  • Are you using Threads? If you are not (you only have your main thread) it is normal that the GUI does not refresh until capture ends. Use a different thread to capture, so that you don't block the GUI Thread. – Salvatorelab Feb 21 '13 at 11:37

1 Answers1

1

I assume you run the code in a thread. Use SwingUtilities.invokeAndWait to call the pcktTextArea.append() code

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Thank you very much for your answer, I have not use threads before and I have to learn that as well but thanks for the answer now I know I have to use threads for this project. – Rojee Feb 22 '13 at 13:43