2

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 
    }
}
Robert
  • 10,403
  • 14
  • 67
  • 117
  • Have you thought about what columns you'll have in the database table where you'll store these? – Dawood ibn Kareem Apr 27 '14 at 19:03
  • What's the actual question here? – user207421 Apr 27 '14 at 19:30
  • how to run savePacket() Method and packetArrived() Method simultaneously –  Apr 27 '14 at 20:20
  • yes sir yes sir my database is ready,i want to increase the efficiency by not doing saving data procedure in packetArrived method –  Apr 27 '14 at 20:21
  • You could put the captured packets into a queue and have the database method read from the queue. But what happens if the queue fills up? The answer to your question seems to be basically 'threads', but surely you knew that? – user207421 Apr 27 '14 at 21:08
  • Thanks for that i have same idea but how can i implement threads in one class containing two methods –  Apr 27 '14 at 21:39
  • With two anonymous Runnables, but why does it have to be one class with two methods? – user207421 Apr 27 '14 at 21:45
  • i am implementing two threds running two different classes. thread 1 runs sniffer class, and thread 2 runns data saving class. –  Apr 27 '14 at 21:55
  • How to implement anonymous Runnables? –  Apr 27 '14 at 22:00
  • Why do you want to have two threads here? It seems to me that the two threads would spend a lot of time waiting for each other, and you wouldn't get any gain from having them. For maximum efficiency, I would suggest running it all in the same thread. – Dawood ibn Kareem Apr 28 '14 at 06:14
  • I want to do capturing and saving in two different threads because , if i do both capturing and saving in one thread it will consume more time and it will lost half of the packets. i don't want to loos packets.i want to run saving Method side by side with capturing without interrupting Capturing Mechanism to capture all packets. –  Apr 28 '14 at 08:50
  • You haven't answered my question. We all agree you need two threads. Why does it have to be cone class with two methods? And if it does, what's the problem with calling those two methods from separate threads? – user207421 Apr 28 '14 at 10:13
  • @EJP I want to run 2 methods in two threads because i cannot interrupt packetArrived method, this method is API defined method, i don't want to make this method heavy, that's why i want to save packets in another method, but i want value of his method to use in other method. What is the way to do this ? –  Apr 28 '14 at 10:47
  • @EJP I believe Alia was answering *my* question, not yours. – Dawood ibn Kareem Apr 28 '14 at 11:34
  • @DavidWallace That's what I said. He hasn't answered my question. – user207421 Apr 28 '14 at 11:57
  • @EJP I didn't understood your Question? –  Apr 28 '14 at 13:00

3 Answers3

0
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();
}
  • I think you'll need to have some kind of loop in your `run` method, otherwise you'll only get one packet from the queue. – Dawood ibn Kareem Apr 28 '14 at 11:37
  • i am getting NUll, not getting even 1 value –  Apr 28 '14 at 12:58
  • Right. I guess your `run` is occurring before anything is added to the queue. You could use a `BlockingQueue`, so you can use the `take` method. But you'll still need a loop as well. – Dawood ibn Kareem Apr 28 '14 at 13:01
  • Have a look at [LinkedBlockingQueue](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingQueue.html) - it might be what you want. – Dawood ibn Kareem Apr 28 '14 at 13:03
  • it Never gives me value in run function,what ever i do. i am handing null exception by a while loop –  Apr 28 '14 at 13:08
  • Is your `packetArrived` method running at all? – Dawood ibn Kareem Apr 28 '14 at 13:10
  • So when it runs, there's still nothing in the queue? – Dawood ibn Kareem Apr 28 '14 at 13:57
  • no If i display queue in packetArrived method it shows result , But in run method it shows null –  Apr 28 '14 at 18:58
  • OK, well right now, you have an endless loop that's going to just keep on writing `null` to the console. So if you have added a few values to the queue, they are probably going to scroll out of sight. Why don't you change it so that it only prints if the value polled is not null? Really though, I think you should use `LinkedBlockingQueue`, and either use the `take` method, or the version of `poll` that has a timeout. – Dawood ibn Kareem Apr 28 '14 at 19:08
  • LinkedBlockingQueue also not working. Not displaying any value –  Apr 29 '14 at 13:26
0

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());
    }

}
}

0

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

4aRk Kn1gh7
  • 4,259
  • 1
  • 30
  • 41