public void testPing() throws IOException {
String address="220.181.111.147";
Process process = Runtime.getRuntime().exec("ping "+address);
InputStreamReader r = new InputStreamReader(process.getInputStream());
LineNumberReader returnData = new LineNumberReader(r);
String returnMsg="";
String line = "";
while ((line = returnData.readLine()) != null) {
System.out.println(line);
returnMsg += line;
}
if(returnMsg.indexOf("100% loss")!=-1){
System.out.println("与 " +address +" 连接不畅通.");
}
else{
System.out.println("与 " +address +" 连接畅通.");
}
}
Java Ping函数
点赞
收藏