1. //测试ip+端口号是否可以连通
    2. public static boolean isHostConnectable(String host, int port) {
    3. Socket socket = new Socket();
    4. try {
    5. socket.connect(new InetSocketAddress(host, port));
    6. } catch (IOException e) {
    7. e.printStackTrace();
    8. return false;
    9. } finally {
    10. try {
    11. socket.close();
    12. } catch (IOException e) {
    13. e.printStackTrace();
    14. }
    15. }
    16. return true;
    17. }
    1. //测试ip+超时时间是否可以连接通
    2. public static boolean isHostReachable(String host, Integer timeOut) {
    3. try {
    4. return InetAddress.getByName(host).isReachable(timeOut);
    5. } catch (UnknownHostException e) {
    6. e.printStackTrace();
    7. } catch (IOException e) {
    8. e.printStackTrace();
    9. }
    10. return false;
    11. }