- 1、本文档共10页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
《Ping_JAVA_测试程序》.doc
package util.net;
import java.util.*;
class PingHelper
{
private static final String CMD = CMD /C PING ;
private Process cmdProcess;
public int ping(String ip){
try{
cmdProcess = Runtime.getRuntime().exec(CMD+ip);
cmdProcess.waitFor();
//System.out.println(cmdProcess.exitValue());
return cmdProcess.exitValue();
}catch(Exception e){
e.toString();
}finally{
if(cmdProcess != null)
cmdProcess.destroy();
}
return -1;
}
public static void main (String[] args) {
PingHelper pm = new PingHelper();
System.out.println(RETURN: +pm.ping(127.0.0.2));
}
}
public class PingMaster implements Runnable
{
private PingHelper pingHelper;
private Integer sleepTime;
private HashMapString,Integer pingMap;
public PingMaster(Integer sleepTime,HashMapString,Integer pingMap){
this.pingHelper = new PingHelper();
this.sleepTime = sleepTime;
this.pingMap = pingMap;
}
public synchronized void setPingStatus(String pingIP,Integer pingStatus){
pingMap.put(pingIP,pingStatus);
}
public HashMapString,Integer getPingStatus(){
return this.pingMap;
}
public void run(){
while(true){
SetString pingIPs = pingMap.keySet();
for(String pingIP : pingIPs){
int pingStatus = pingHelper.ping(pingIP);
setPingStatus(pingIP,pingStatus);
}
try{
Thread.sleep(sleepTime);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public static void main (String[] args) throws InterruptedException {
HashMapString,Integer map = new HashMap();
map.put(192.168.0.1,1);
map.put(192.168.2.32,1);
map.put(127.0.0.1,1);
PingMaster p = new PingMaster(2000,map);
Thread t = new Thread(p);
t.join();
t.start();
while(true){
map = p.getPingStatus();
SetString set = map.keySet();
String datetime = (new Date()).toLocaleString();
for(String s : set){
System.out.println(*-*-* +datetime + Ping-Report *-*-*);
System.out.println(Ping + Status: +map.get(s));
Thread.sleep(1500)
文档评论(0)