ping - Pinging a list of IPs in Android -


i'm programming android app.

i have activity responsable on pinging 254 ipv4 , putting connected machines in database table ( putting ip )when button "auto scan" clicked. //---------------------------------------------------------------------

first local ip ( exemple 192.168.1.8), when button clicked, extract substring ip ( 192.168.1. exemple) start pinging ips starting "192.168.1.1" , finishing "192.168.1.254". after every ping, make test know if ping succeeded or failed, if succeeded put ip in database table, show list of connected ips in listview. //---------------------------------------------------------------------

the problem pinging task taking long finish ( 15min or more !!), it's crazy. tried use inetaddress.isreachable() fast, can't find computer working windows, changed work process & runtime.getruntime().exec(cmd).

below android code :

class mytask extends asynctask<string, integer, void> {               dialog dialog;     progressbar progressbar;     textview tvloading,tvper;     button btncancel;      @override     protected void doinbackground(string... params) {         thread.currentthread().setpriority(thread.max_priority);          string s="", address = params[0];         int index=0,j=0,count=0;          //on prend le dernier index du char "."         final stringbuilder ss = new stringbuilder(address);         index = ss.lastindexof(".");         //on prend une souschaîne qui contient l'ipv4 sans le dernier nombre         s = address.substring(0,index+1);          //tester tous les adresses ipv4 du même plage d'adresses         for(int i=1; i<255; i++){             if (iscancelled()) {                 break;             }             if(testping(s+""+i+"")){                 insertipv4(s+""+i+"");}             count++;             if(count == 5 && j<100){                 count=0;                 j+=2;                 publishprogress(j);             }         }         return null;     }      @override     protected void onpostexecute(void result){         super.onpostexecute(result);          dialog.dismiss();          alertdialog alert = new alertdialog.builder(gestion_machines.this)                 .create();          alert.settitle("terminé");         alert.setmessage("operation terminé avec succés");         alert.setbutton("ok", new dialoginterface.onclicklistener() {              @override             public void onclick(dialoginterface dialog, int which) {                 dialog.dismiss();             }         });         alert.show();     }      @override     protected void onpreexecute() {         super.onpreexecute();         dialog = new dialog(gestion_machines.this);         dialog.setcancelable(false);         dialog.requestwindowfeature(window.feature_no_title);         dialog.setcontentview(r.layout.progressdialog);          progressbar = (progressbar) dialog.findviewbyid(r.id.progressbar1);         tvloading = (textview) dialog.findviewbyid(r.id.tv1);         tvper = (textview) dialog.findviewbyid(r.id.tvper);         btncancel = (button) dialog.findviewbyid(r.id.btncancel);          btncancel.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 objmytask.cancel(true);                 dialog.dismiss();             }         });          dialog.show();     }      @override     protected void onprogressupdate(integer... values) {         super.onprogressupdate(values);         progressbar.setprogress(values[0]);         tvloading.settext("loading...  " + values[0] + " %");         tvper.settext(values[0]+" %");     } } 

and method make ping :

public boolean testping(string x){         int exit=22;         process p;          try {             //.....edited line.....              p = runtime.getruntime().exec("ping -c1 -w1 "+x);             //make shure -w not -w it's not same.              p.waitfor();             exit = p.exitvalue();             p.destroy();         } catch (ioexception ee) {             ee.printstacktrace();         } catch (interruptedexception e) {             e.printstacktrace();         }         if (exit == 0) {             return true;         }else{             return false;         }     } 

i searched on net, founf in java, have used command called " fping " accept timeout option ( -t) milliseconds. ping command in android ( shell ) can accept timeout seconds. so, case, hole operation should take 254 seconds, not bad .... not perfect.

public boolean testping(string x){     int exit=22;     process p;      try {         //.....edited line.....          p = runtime.getruntime().exec("ping -c1 -w1 "+x);         //make shure -w not -w it's not same.          p.waitfor();         exit = p.exitvalue();         p.destroy();     } catch (ioexception ee) {         ee.printstacktrace();     } catch (interruptedexception e) {         e.printstacktrace();     }     if (exit == 0) {         return true;     }else{         return false;     } } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -