轉:https://www.cnblogs.com/chenchaochao/p/5522654.html
判斷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; }
判斷ip連通性
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; }