android - Speeding up Ip Scanner with ExecutorService reporting wrong ips -
i'm working on ip scanner scan local net live hosts. implementation uses asynctask run in background , excutorservice create pool thread faster results; however, i'm getting wrong ips. eg ips not exist on network. implementing right? how spawn multiple runnable threads , @ same time update right output. thanks
/* * ascyntask scan network * should try different timeout network/devices * try detect localhost ip addres , subnet. * use subnet scan network */ private class taskscannetwork extends asynctask<void, node, void> { string subnet; static final int lower = 1; static final int upper = 254; static final int timeout = 1000; private executorservice ipscanpool; @override protected void onpreexecute() { log.v("ip address: ", ip); subnet = ip.substring(0, ip.lastindexof(".")); hostlist.clear(); scanprogress.setmax(upper); ipscanpool = executors.newfixedthreadpool(upper); statusmsg.settext("scanning " + subnet + ".0/24"); } @override protected void doinbackground(void... params) { (int = lower; <= upper; i++) { ip = subnet + "." + i; log.v("host: ", ip); final node newnode = new node(); newnode.progressbar = i; ipscanpool.execute(new runnable() { @override public void run() { try { inetaddress inetaddress = inetaddress.getbyname(ip); if (inetaddress.isreachable(timeout)) { newnode.ip = ip; } } catch (unknownhostexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } publishprogress(newnode); } }); } return null; } @override protected void onprogressupdate(node... values) { //only add arraylist<node> if host 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"); } }
Comments
Post a Comment