JAVA判断ip、端口是否可连接
1.判断ip、端口是否可连接
public static boolean isHostConnectable(String host, int port) {
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(host, port));
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
2.判断ip是否可以连接 timeOut是超时时间
public static boolean isHostReachable(String host, Integer timeOut) {
try {
return InetAddress.getByName(host).isReachable(timeOut);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
作者:ccc
本文版权归作者和博客园共有,欢迎转载,但必须在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步!
posted on 2016-05-24 10:51 laugher_ccc 阅读(36364) 评论(0) 收藏 举报
浙公网安备 33010602011771号