
1、ping-ip
//创建连接 验证ip端口
public static boolean isReachableIp(String ipAddress) {
try {
InetAddress inetAddress = InetAddress.getByName(ipAddress);
if (inetAddress.isReachable(5000)) {
return true;
} else {
return false;
}
} catch (IOException e) {
return false;
}
}
2、telnet-端口
//创建连接 验证ip端口端口
public static boolean isReachable(String ipAddress, int port) {
try {
Socket socket = new Socket(ipAddress, port);
socket.close();
return true;
} catch (IOException e) {
return false;
}
}
文章介绍了使用Java编写的两个方法,isReachableIp()通过ping检查IP地址的可达性,isReachable()通过telnet连接验证指定IP和端口的可达性,处理IOException异常。
1889

被折叠的 条评论
为什么被折叠?



