简体   繁体   中英

capturing packets from network and saving in database using jpcap library

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 
    }
}
public class PacketCapture implements PacketListener,Runnable {
Queue<Packet> queue = new LinkedList<>();
@Override
public void packetArrived(Packet packet) 
{
   queue.add(packet);
   System.out.println(m_counter++);
}

public void run()
{
  while(!(queue.equals(null))){
    System.out.println(queue.poll());
 }
 }
 }

In Main Method

 public static void main(String[] args) {

     Thread t1 = new Thread(new PacketSniffer());
    Thread t2 = new Thread(new PacketCapture());
     t1.start();
    t2.start();
}

Now whats he problem with this axample it is also not displaying value in Runnable1 class

 public class Test {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

 Thread t1 = new Thread(new Runnable1());
 t1.start();
  Thread t2 = new Thread(new RunnableDemo());
 t2.start();
}
}

 class RunnableDemo implements Runnable {
Queue queue = new LinkedList<>();
public void run() 
{
   queue.add(4);
    queue.add(8);
     queue.add(12);
   System.out.println("Running");
}

 }
 class Runnable1 implements Runnable {
public void run()
  {
    RunnableDemo RunnableDemo = new RunnableDemo();
    for(int i=0; i<=100; i++)
    {
    System.out.println(RunnableDemo.queue.poll());
    }

}
}

Well alia what you can do over here is have two threads running simultaneously one for packet capturing and one for storing the packets in the database. You can use any messaging library like zeroMQ or rabbitMQ to asynchronously send packets to the database thread as soon as they are received which would automatically handle any queuing. In this way your packet capture won't be blocked and you would be able to do both the things simultaneously. Your only bottleneck would be your database insertion speed which can be improved if you use a nosql or mongodb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM