List<String> ipList = new ArrayList<String>();
public static String locAddress;//存储本机ip,例:本地ip :192.168.1.
private Runtime run = Runtime.getRuntime();//获取当前运行环境,来执行ping,相当于windows的cmd
private Process proc = null;
private String ping = "ping -c 1 -w 0.5 " ;//其中 -c 1为发送的次数,-w 表示发送后等待响应的时间
private int j;//存放ip最后一位地址 0-255
locAddress = getLocAddrIndex();//获取本地ip前缀
if(locAddress.equals("")){
Toast.makeText(this, "扫描失败,请检查wifi网络", Toast.LENGTH_LONG).show();
return ;
}
for ( int i = 2; i < 255; i++) {//创建253个线程分别去ping
j = i ;
new Thread(new Runnable() {
public void run() {
// ipList.add(j+" ");
String p = ping + locAddress + j ;
String current_ip = locAddress+ j;
try {
proc = run.exec(p);
int result = proc.waitFor();
if (result == 0) {
ipList.add(current_ip)
; Log.i("faxian ", "faxian: "+current_ip);
} else {
}
} catch (Exception e) {
e.printStackTrace();
}finally {
proc.destroy();
}
}
}).start();
//方法没有错,但是测试时,很不稳定经常出不来十次都不一定能成功一次,成功时列表只有一个或两个IP,不能把全部IP列出来,是什么问题?求助
public static String locAddress;//存储本机ip,例:本地ip :192.168.1.
private Runtime run = Runtime.getRuntime();//获取当前运行环境,来执行ping,相当于windows的cmd
private Process proc = null;
private String ping = "ping -c 1 -w 0.5 " ;//其中 -c 1为发送的次数,-w 表示发送后等待响应的时间
private int j;//存放ip最后一位地址 0-255
locAddress = getLocAddrIndex();//获取本地ip前缀
if(locAddress.equals("")){
Toast.makeText(this, "扫描失败,请检查wifi网络", Toast.LENGTH_LONG).show();
return ;
}
for ( int i = 2; i < 255; i++) {//创建253个线程分别去ping
j = i ;
new Thread(new Runnable() {
public void run() {
// ipList.add(j+" ");
String p = ping + locAddress + j ;
String current_ip = locAddress+ j;
try {
proc = run.exec(p);
int result = proc.waitFor();
if (result == 0) {
ipList.add(current_ip)
; Log.i("faxian ", "faxian: "+current_ip);
} else {
}
} catch (Exception e) {
e.printStackTrace();
}finally {
proc.destroy();
}
}
}).start();
//方法没有错,但是测试时,很不稳定经常出不来十次都不一定能成功一次,成功时列表只有一个或两个IP,不能把全部IP列出来,是什么问题?求助