Here you can find the source of isReachable(final String hostName, int port, int timeout)
Parameter | Description |
---|---|
hostName | Hostname |
port | Port |
timeout | Timeout in ms |
public static boolean isReachable(final String hostName, int port, int timeout)
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; public class Main { /**//from w ww . ja v a 2 s .c o m * Checks that given hostname is reacheble in certain timeout. * Basically working as {@link java.net.InetAddress#isReachable(int)}, but working on all platforms * * @param hostName Hostname * @param port Port * @param timeout Timeout in ms * @return Reachable or not */ public static boolean isReachable(final String hostName, int port, int timeout) { try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(hostName, port), timeout); return true; } catch (IOException ignore) { return false; } } }