stealthcopter / AndroidNetworkTools

Set of useful android network tools
Apache License 2.0
1.36k stars 283 forks source link

How to stop or cancel port scanning in middle of work ? #27

Closed eakteam closed 6 years ago

eakteam commented 6 years ago

Just want to know how to stop scanning ?

stealthcopter commented 6 years ago

just call the cancel() method

eakteam commented 6 years ago

Hello, thank you for your reply. I know that , i have read how it works but where to call cancel() method outside of this thread ?

new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        startTime = System.currentTimeMillis();

                                        PortScan.onAddress(text_ip.getText().toString()).setTimeOutMillis(select_timeout).doScan(new PortScan.PortListener() {
                                            @Override
                                            public void onResult(final int i, boolean b) {
                                                if (b) {
                                                    received_tcp++;

                                                 }
                                            }
                                            @Override
                                            public void onFinished(ArrayList<Integer> arrayList) {

                                            }
                                        });
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }).start();

I have put it inside the new Thread because it throws exception : Network on Main Thread. I cannot initialize this class e.x PortScan prtsc = new PortScan(); because it says have private access.

eakteam commented 6 years ago

I solve it by declaring PortScanner prt = null;

and after doing all ports scan i can call it by prt.cancel();

Thank you !

stealthcopter commented 6 years ago

You should solve this by keeping a reference to the PortScan object that is created like so:

// Create PortScan object
PortScan portScan = PortScan.onAddress(....)...

// Then call cancel when needed
portScan.cancel()