码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA判断ip、端口是否可连接

时间:2016-05-24 11:46:01      阅读:908      评论:0      收藏:0      [点我收藏+]

标签:

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;
	}

  

JAVA判断ip、端口是否可连接

标签:

原文地址:http://www.cnblogs.com/chenchaochao/p/5522654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!