1

I'm working on a packet sniffer but I'm getting trouble about the way to stop a thread (without deprecated methods) containing a blocking method.

The concerned method is the loop() one from pcap4j library. As it is a blocking method, I put it into a thread to keep the main one working. However, in order to apply a filter to the pcap interface, I have to break the loop and restart it as the breakloop() function of the library returns an InterruptedException. So my idea was to kill the thread containing the method. But as I can't get into the library's loop causing the method to block, I'm unable to do it by checking whether the thread is interrupted or not.

Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
        loop(args);
    }
});
t.start();

How can I stop it ?

EDIT : What I did was to recompile the library from its source, removing the InterruptedException from the PcapHandle class.

GoldenBolt
  • 51
  • 8
  • The code, do you have the full part of it? The whole encompassing class or link to source? – Mr00Anderson Apr 11 '19 at 23:05
  • Not now sorry but the only interesting part is there I guess (I really tried everything). – GoldenBolt Apr 11 '19 at 23:09
  • 1
    Can you show some code how you would have done it if the `Thread#stop` was not deprecated? Also, what's wrong with using `breakLoop`? – VHS Apr 11 '19 at 23:20
  • If ```stop``` was not deprecated, I would have called it from the main thread, then applied the filter to my ```pcaphandle``` and started a new thread. Concerning ```breakloop``` method, it returns an ```InterruptedException```. – GoldenBolt Apr 11 '19 at 23:30
  • I found some library that I think you are using which has API by the way and source available. We cannot help you if we cannot see the library or use case specifics? Anytime a thread can be interrupted that is the except that gets thrown. – Mr00Anderson Apr 11 '19 at 23:34
  • The library is ```pcap4j``` and the class used to perform pcap actions is called ```PcapHandle```. Here is the documentation : https://kaitoy.github.io/pcap4j/javadoc/latest/en/org/pcap4j/core/PcapHandle.html – GoldenBolt Apr 11 '19 at 23:40

1 Answers1

2

Using the Thread#getAllStackTraces you can obtain all the threads. SO Has a few other answers to this as well for getting a hold of the threads. After finding the thread you can interrupt it. The Thread.class also has some other identifiers that may help find the thread too.

Edit: Are you using kaitoy/pcap4j ? if so breakLoop() Nothing can be done about the InterruptedException, thats how the library intended for it to be broken. I would look at putting an issue in with their github if they need to implement a feature.

import java.util.Set;

class TestStuff {

    public static void main(String[] args) {
        Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
        for (Thread thread : threadSet) {
            if(thread.getName().equals("some-name")){
                thread.interrupt();
            }
        }
    }
}

Mr00Anderson
  • 854
  • 8
  • 16
  • 1
    Thank you but the problem is that the interrupt method as no other effect than changing the boolean Thread.isInterrupted() which is useless in my case. – GoldenBolt Apr 11 '19 at 23:24
  • Why not? That exception that thrown is for that? Have you tried? – Mr00Anderson Apr 11 '19 at 23:30
  • 1
    Now I wonder if I tried to call ```interrupt``` and then ```breakloop``` (which would be ridiculous I admit) I will try that tomorrow (1h34 in France) but thank you for all. – GoldenBolt Apr 11 '19 at 23:34
  • 1
    I saw your edit and I'm going to post this on their github thanks a lot for your work. – GoldenBolt Apr 13 '19 at 21:33