概述
java怎样去判断某个ip地址网络是否良好
可以通过InetAddress类和ping包方式
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HeartBeat {
//使用InetAddress类
public boolean method1(String ipAddress) throws Exception {
InetAddress host = InetAddress.getByName(ipAddress); //指定主机名称
boolean status = host.isReachable(3000); //3000ms为超时时间
System.out.println("请求主机IP地址:" + host.getHostAddress() + " t STATUS:" + status);
return status;
}
/**
* 调用控制台ping命令,完整逻辑
*
* @param ipAddress 请求IP地址
* @param countNum 请求连接数量
* @return status 连接状态
*/
public String method2(String ipAddress, int countNum) {
String command;
String line;
String packetLoss;
String status = "";
String timeout = "";
BufferedReader br = null;
String osName = System.getProperty("os.name");
if (osName.toLowerCase().contains("linux")) {
command = "ping " + ipAddress + " -c " + countNum;
try {
Process p = Runtime.getRuntime().exec(command);
br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
while ((line = br.readLine()) != null) {
boolean bl = !"".equals(line);
// 英文linux OS
if (line.contains("packet loss") && bl) {
packetLoss = line.split(",")[2].replace("packet loss", "").trim();
status = ipAddress + "---->丢包率:" + packetLoss;
} else if (line.contains("min/avg/max/mdev") && bl) {
timeout = line.split("=")[1].split("/")[1];
status += "---->延时:" + timeout + "ms";
}
}
//System.out.println(status);
return status;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} else if (osName.toLowerCase().contains("windows")) {
command = "ping " + ipAddress + " -n " + countNum;
try {
Process p = Runtime.getRuntime().exec(command);
br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
while ((line = br.readLine()) != null) {
boolean bl = !"".equals(line);
// 中文windows OS
if (line.contains("丢失") && bl) {
packetLoss = line.split("\(")[1].replace("丢失),","").trim();
status = ipAddress + "---->丢包率:" + packetLoss;
} else if (line.contains("平均") && bl) {
timeout = line.split("=")[3].trim();
status += "---->延时:" + timeout;
}
}
//System.out.println(status);
return status;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return "This system is not linux or windows!";
}
/**
* 调用控制台ping命令,完整逻辑
* @param ipAddress 请求IP地址
* @param countNum 请求连接数量
* @return 是否成功连接
*/
public boolean method3(String ipAddress, int countNum) {
String osName = System.getProperty("os.name");
String command;
if (osName.contains("Windows")){
command = "ping " + ipAddress + " -n "+ countNum;
}else{
command = "ping " + ipAddress + " -c 100 -i 0";
}
System.out.println(command);
String line;
int connectedCount = 0;
BufferedReader br = null;
try {
Process p = Runtime.getRuntime().exec(command);
br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
while ((line = br.readLine()) != null) {
connectedCount += getCheckResult(line);
}
return (connectedCount/countNum) > 0.8 ;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
try{
br.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
private static int getCheckResult(String line){
Pattern pattern = Pattern.compile("(\d+ms)(\s+)(TTL=\d+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
while (matcher.find()){
return 1;
}
return 0;
}
}
最后
以上就是幸福犀牛为你收集整理的心跳检测--判断网络状态是否良好的全部内容,希望文章能够帮你解决心跳检测--判断网络状态是否良好所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复