Results 1 to 2 of 2
  1. #1
    miatech is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default how can I update onprogressupdate in asynctask when doing a ping scan

    guys I'm working on application that scan the net for ip's addresses. For this I'm using Asynctask and spawning a number of threads with ExecutorService and Future interface. But I'm having problems updating the onProgessUpdate() method of AsyncTask. This method is receiving an object of type Node. And the IpScanCallable interface also returns same object; however, it is not working. Am I missing the correct way to do this? If someone can help appreciated it. Thanks
    Java Code:
    /*
        * AscynTask to scan the network
        * you should try different timeout for your network/devices
        * it will try to detect localhost ip addres and subnet. then
        * it will use subnet to scan network
        */
        private class TaskScanNetwork extends AsyncTask<Void, Node, Void> {
            static final int SCAN_THREADS = 8;
            static final int numOfHost = 254;
            String subnet = ip.substring(0, ip.lastIndexOf("."));;
            int startIp = 1;
            int endIp = startIp + SCAN_THREADS;
            static final int timeout = 1000;
            ExecutorService ipScanExecutor = Executors.newFixedThreadPool(SCAN_THREADS);
            List<Future<Node>> ipList = new ArrayList<>();
    
    
            @Override
            protected void onPreExecute() {
                Log.v("Ip Address: ", ip);
                hostList.clear();
                scanProgress.setMax(numOfHost);
                statusMsg.setText("Scanning " + subnet + ".0/24");
            }
    
            @Override
            protected Void doInBackground(Void... params) {
                for (int i = 0; i <= SCAN_THREADS; i++) {
                    //if canceled terminate AsyncTask
                    if(isCancelled()) {
                        break;
                    }
                    IpScanCallable ipScanCallable = new IpScanCallable(subnet, startIp, endIp);
                    Future<Node> futureResults = ipScanExecutor.submit(ipScanCallable);
                    startIp = endIp + 1;
                    endIp = startIp + SCAN_THREADS;
                    ipList.add(futureResults);
                }
    
                ipScanExecutor.shutdown();
    
                publishProgress();
                return null;
            }
    
            @Override
            protected void onProgressUpdate(Node... values) {
                //only add to ArrayList<Node> if host is alive on network
                if(values[0].ip != null) {
                    hostList.add(values[0]);
                    networkAdapter.notifyDataSetInvalidated();
                    Toast.makeText(getApplicationContext(), values[0].getIp(), Toast.LENGTH_SHORT).show();
                }
                //update progress bar
                scanProgress.setProgress(values[0].progressBar);
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                statusMsg.setText(subnet + ".0/24 " + " Hosts");
            }
        }
    IpScanCallable class
    Java Code:
    package com.example.android.droidscanner;
    
    import android.util.Log;
    
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.concurrent.Callable;
    
    /**
     * Created by jlvaz on 3/28/2017.
     */
    
    public class IpScanCallable implements Callable<Node> {
        private Node newNode;
        private int timeout = 1000;
        private int progress;
        private String ip;
        private String network;
        private int startIp;
        private int endIp;
    
        public IpScanCallable(String network, int startIp, int endIp) {
            this.network = network;
            this.startIp = startIp;
            this.endIp = endIp;
        }
    
        @Override
        public Node call() throws Exception {
            for(int i = startIp; i <= endIp; i++) {
                ip = network + "." + i;
                Log.v("Host: ", ip);
    
                newNode = new Node();
                newNode.progressBar = i;
    
                try {
                    InetAddress inetAddress = InetAddress.getByName(ip);
                    if (inetAddress.isReachable(timeout)) {
                        newNode.ip = ip;
                    }
    
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return newNode;
        }
    
    }
    Last edited by miatech; 03-29-2017 at 07:59 PM. Reason: formatting code

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: how can I update onprogressupdate in asynctask when doing a ping scan

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. AsyncTask problem
    By Zarah in forum Android
    Replies: 7
    Last Post: 04-08-2014, 07:09 AM
  2. AsyncTask and ProgressDialog
    By Glorfindel Arnatuile in forum Android
    Replies: 1
    Last Post: 04-10-2012, 03:22 AM
  3. Replies: 6
    Last Post: 04-06-2012, 08:29 AM
  4. Scan 1.3
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 05-13-2008, 05:19 PM
  5. Scan 1.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 09-22-2007, 11:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •