Problem with isReachable in InetAddress class

前端 未结 3 1172
你的背包
你的背包 2020-12-03 17:51

As an assignment I have to find all the alive computers on a LAN. For which I am using isReachable function of InetAddress class. But problem is

3条回答
  •  温柔的废话
    2020-12-03 18:22

    Here is the code which is platform independent, but requires information about any open port on the other machine (which we have most of the time).

    private static boolean isReachable(String addr, int openPort, int timeOutMillis) {
        // Any Open port on other machine
        // openPort =  22 - ssh, 80 or 443 - webserver, 25 - mailserver etc.
        try {
            try (Socket soc = new Socket()) {
                soc.connect(new InetSocketAddress(addr, openPort), timeOutMillis);
            }
            return true;
        } catch (IOException ex) {
            return false;
        }
    }
    

提交回复
热议问题