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?