I am capturing packets using jpcap library and saving in Mysql database. I want to do these two functions separately. My program captures packet and saves in database then captures another packet and saves in database. I want is that one Method capture packets and another Method saves in database. Saving packet does not stop capturing to complete the process.
public class PacketSniffer {
private static String[] devices;
private static PacketCapture captor;
private static Packet info;
private static final Scanner input = new Scanner(System.in);
private static final String FILTER = "";
private static final int PACKET_COUNT = -1;
public PacketSniffer()
{
captor = new PacketCapture();
int i;
devices = PacketCapture.lookupDevices();
for(i=0; i<devices.length; i++)
{
System.out.println(i+": "+devices[i]); // +devices[i].name
System.out.println();
}
String device = input.nextLine();
captor.open(device, 65535, true, 0);
captor.setFilter(FILTER, true);
captor.addPacketListener(new PacketCapture());
captor.capture(PACKET_COUNT);
}
}
Packet Handler to handle captures packets:
public class PacketHandler implements PacketListener {
Queue<Packet> queue;
@Override
public void packetArrived(Packet packet)
{
System.out.println(packet);
}
public void savePacket()
{
// Method to save packet in database
}
}