日期:2014-05-20  浏览次数:20845 次

我写了一个程序是扫描在内网中所有的可用IP,可是,在my Eclipse中一运行就会死机!!!代码如下:::
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();
}
}
}
请各位给个意见!!!

------解决方案--------------------
主线程结束了,子线程还没运行完?
------解决方案--------------------
因为主程序中pingMap是空的呗

相关资料更多>