I'm programming an android APP.
I have an activity responsable on Pinging 254 IPv4 and Putting the connected machines in a Database Table ( putting the ip )when a Button "Auto Scan" is clicked. //---------------------------------------------------------------------
First i get the Local IP ( for exemple 192.168.1.8), then when the button clicked, i extract a subString from the ip ( 192.168.1. for this exemple) then start pinging all the IPs starting from "192.168.1.1" and finishing with "192.168.1.254". after every ping, i make a test to know if that ping succeeded or failed, if it succeeded then i put that ip in a database table, then finally i show the list of connected IPs in a listview. //---------------------------------------------------------------------
The problem is that the Pinging task is taking too long to finish ( about 15min or more !!), it's crazy. I tried to use InetAddress.isReachable() it was fast, but it can't find a computer working with windows, so i changed to work with Process & Runtime.getRuntime().exec(cmd).
Below the 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 The Method that make the ping :
public boolean testping(String x){
int exit=22;
Process p;
try {
//.....edited line.....
p = Runtime.getRuntime().exec("ping -c1 -W1 "+x);
//make shure that is -W not -w it's not he 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;
}
}