stealthcopter / AndroidNetworkTools

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

PortScan() blocked UI thread and caused Progress Dialog is stopped and hang #58

Closed jerrychong25 closed 4 years ago

jerrychong25 commented 4 years ago

Hi all,

I have implemented PortScan() function successfully.

However, it will block my Progress Dialog when running PortScan() function, whereby my Progress Dialog is called before PortScan() function.

The Progress Dialog is stopped and hang half way until the PortScan() is executed completely.

Is there any way to prevent blocking of my Progress Dialog when running PortScan() function?

Thanks.

My Source Code:

// Find Port Number Asynchronously
PortScan.onAddress("192.168.1.11").setTimeOutMillis(500).setPortsAll().setMethodTCP().doScan(object:

    PortScan.PortListener {
        override fun onResult(portNum:Int, open:Boolean) {
            if (open) {
                // Found new open port
            }
        }

        override fun onFinished(openPorts:ArrayList<Int>) {
            // Finished Scanning
            if(openPorts.isEmpty()) {

            }
            else {

            }
        }
    }
)
jerrychong25 commented 4 years ago

Similar issue: https://github.com/stealthcopter/AndroidNetworkTools/issues/51

jerrychong25 commented 4 years ago

@SemenovAlexander

Any updates on this?

Thanks.

stealthcopter commented 4 years ago

@jerrychong25 @SemenovAlexander Hi Both,

I've used the following code on android 6.0 and 9.0 and do not get UI blocking. I'll run some tests on < 6.0 and see what I get. Do you still get the blocking if you set the thread count to a low number? might be worth testing with 1.

        // Find Port Number Asynchronously
        PortScan.onAddress("192.168.0.50").setTimeOutMillis(500).setPortsAll().setMethodTCP().doScan(new PortScan.PortListener() {
            @Override
            public void onResult(int portNo, boolean open) {
                Log.e("TEST","Result: "+portNo+" "+open);
            }

            @Override
            public void onFinished(ArrayList<Integer> openPorts) {
                Log.e("TEST","Result: "+openPorts.size());
            }
        });
jerrychong25 commented 4 years ago

@jerrychong25 @SemenovAlexander Hi Both,

I've used the following code on android 6.0 and 9.0 and do not get UI blocking. I'll run some tests on < 6.0 and see what I get. Do you still get the blocking if you set the thread count to a low number? might be worth testing with 1.

        // Find Port Number Asynchronously
        PortScan.onAddress("192.168.0.50").setTimeOutMillis(500).setPortsAll().setMethodTCP().doScan(new PortScan.PortListener() {
            @Override
            public void onResult(int portNo, boolean open) {
                Log.e("TEST","Result: "+portNo+" "+open);
            }

            @Override
            public void onFinished(ArrayList<Integer> openPorts) {
                Log.e("TEST","Result: "+openPorts.size());
            }
        });

No issue on low number.

But in my application use case, I need to set setPortsAll() function so is there any other workaround for this?

rmirabelle commented 4 years ago

Related to this issue...

In my case, my Activity calls a method called scan_subnet(), which does a subnet scan. When calling scan_subnet() from onCreate(), the Activity hasn't finished rendering its UI yet, and so freezes/locks up/blocks until the scan is complete.

I was able to fix this by hooking into the onWindowFocusChanged event instead of onCreate or onResume or even onAttachedToWindow, each of which blocks the UI before the Activity is fully rendered.

//kotlin
override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)
    if(hasFocus) {
        scan_subnet()
    }
}

This feels a bit hacky, but works for my needs.

I would like this library to handle the UI blocking issues more elegantly, without having to manually deal with threading concerns.

jerrychong25 commented 4 years ago

No longer using AndroidNetworkTools nowadays. Will close this issue.

Thanks all!