package zepc.web;import java.net.InetAddress;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;public class GetAll {
public static void main(String args[]){
try{
HashMap<String, String> pingMap = new HashMap<String, String>();
Set<String> set;
InetAddress host=InetAddress.getLocalHost();
String hostAddress=host.getHostAddress();
int pos=hostAddress.lastIndexOf(".");
String wd=hostAddress.substring(0, pos+1);
for(int i=1;i<=4;i++){
String ip=wd+i;
//System.out.println(ip);
PingIpThread thread=new PingIpThread(ip,pingMap);
thread.start();
}
set=pingMap.keySet();
Iterator<String> it=set.iterator();
while(it.hasNext()){
String key=it.next();
String value=pingMap.get(key);
if(value.equals("true")){
System.out.println(key+"\n");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
package zepc.web;import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;public class PingIpThread extends Thread{
String ip;
HashMap<String, String> pingMap;
public PingIpThread(String ip,HashMap<String, String> pingMap){
this.ip=ip;
this.pingMap=pingMap;
}
public void run(){
try{
Process process=Runtime.getRuntime().exec("ping "+ip+" -w 280 -n 1");
InputStream is=process.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader in=new BufferedReader(isr);
String line=in.readLine();
while(line!=null){
if(line!=null&&!line.equals("")){
if(line.substring(0,2).equals("来自")||(line.length()>10&&line.substring(0, 10).equals("Reply from"))){
pingMap.put(ip,"true");
}
}
line=in.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
请各位给个意见!!!

解决方案 »

  1.   

    InetAddress host = InetAddress.getLocalHost();
    String hostAddress = host.getHostAddress();
    int pos = hostAddress.lastIndexOf(".");
    String wd = hostAddress.substring(0, pos + 1);
    for (int i = 1; i <= 255; i++) {
    String ip = wd + i;
    Process process = Runtime.getRuntime().exec("ping " + ip + " -w 280 -n 1");
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = in.readLine();
    while ((line = in.readLine()) != null) {
    System.out.println(ip+":"+line);
    }
    }