Why does InetAddress.isReachable return false, when I can ping the IP address?

后端 未结 10 1474
陌清茗
陌清茗 2020-11-22 12:24
InetAddress byName = InetAddress.getByName("173.39.161.140");
System.out.println(byName);
System.out.println(byNam         


        
相关标签:
10条回答
  • 2020-11-22 12:37

    InetAddress.isReachable is flappy, and sometimes returns unreachable for addresses which we can ping.

    I tried the following:

    ping -c 1 <fqdn> and check the exit status.

    Works for all the cases i had tried where InetAddress.isReachable doesn't work.

    0 讨论(0)
  • 2020-11-22 12:38

    I am not sure what was the state when the original question was asked back in 2012.

    As it stands now, ping will be executed as a root. Through the ping executable's authorization you will see the +s flag, and the process belonging to root, meaning it will run as root. run ls -liat on where the ping is located and you should see it.

    So, if you run InetAddress.getByName("www.google.com").isReacheable(5000) as root, it should return true.

    you need proper authorizations for the raw socket, which is used by ICMP (the protocol used by ping)

    InetAddress.getByName is as reliable as ping, but you need proper permissions on the process to have it running properly.

    0 讨论(0)
  • 2020-11-22 12:38

    Or using this way:

    public static boolean exists(final String host)
    {
       try
       {
          InetAddress.getByName(host);
          return true;
       }
       catch (final UnknownHostException exception)
       {
          exception.printStackTrace();
          // Handler
       }
       return false;
    }
    
    0 讨论(0)
  • 2020-11-22 12:42

    If you only want to check if it is connected to internet use this method , It returns true if internet is connected, Its preferable if you use the address of the site you are trying to connect through the program.

         public static boolean isInternetReachable()
        {
            try {
                //make a URL to a known source
                URL url = new URL("http://www.google.com");
    
                //open a connection to that source
                HttpURLConnection urlConnect = (HttpURLConnection)url.openConnection();
    
                //trying to retrieve data from the source. If there
                //is no connection, this line will fail
                Object objData = urlConnect.getContent();
    
            } catch (Exception e) {              
                e.printStackTrace();
                return false;
            }
    
            return true;
        }
    
    0 讨论(0)
  • 2020-11-22 12:44

    I came here to get an answer for this same question, but I was unsatisfied by any of the answers because I was looking for a platform independent solution. Here is the code which I wrote and 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;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 12:45

    Since you can ping the computer, your Java process should run with sufficient privileges to perform the check. Probably due to use of ports in the lower range. If you run your java program with sudo/superuser, I'll bet it works.

    0 讨论(0)
提交回复
热议问题