Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the quickest way to detect an unreachable host in Java?
    primarykey
    data
    text
    <p>I would like the fastest and most accurate function <code>boolean isReachable(String host, int port)</code> that passes the following JUnit tests under the conditions below. Timeout values are specified by the JUnit test itself, and may be considered "unreachable."</p> <p><strong>Please note:</strong> All answers must be platform-independent. This means that <code>InetAddress.isReachable(int timeout)</code> is not going to work, since it relies on port <code>7</code> to do a ping on Windows (ICMP ping being an undocumented function on Windows), and this port is blocked in this setup.</p> <p>LAN Setup:</p> <ul> <li><code>thisMachine</code> (<code>192.168.0.100</code>)</li> <li><code>otherMachine</code> (<code>192.168.0.200</code>)</li> <li><strong>no</strong> machine is called <code>noMachine</code> or has the IP <code>192.168.0.222</code> (always unreachable)</li> <li>both machines are running Apache Tomcat on port <code>8080</code>; all other ports are unreachable (including port <code>7</code>)</li> <li><code>example.com</code> (<code>208.77.188.166</code>) is running a webserver on port <code>80</code> and is only reachable when the LAN is connected to the Internet</li> </ul> <p>Occasionally, the LAN is disconnected from the Internet in which case only local machines called by IP address are reachable (all others are unreachable; there's no DNS).</p> <p><strong>All tests are run on <code>thisMachine</code>.</strong></p> <pre><code>@Test(timeout=1600) // ~320ms per call (should be possible to do better) public void testLocalhost() { // We can always reach ourselves. assertTrue(isReachable("localhost", 8080)); assertTrue(isReachable("127.0.0.1", 8080)); assertTrue(isReachable("thisMachine", 8080)); // Even if there's no DNS! assertTrue(isReachable("192.168.0.100", 8080)); assertFalse(isReachable("localhost", 80)); // Nothing on that port. } @Test(timeout=5500) // ~1867ms per call (should be able to do better) public void testLAN() { assertTrue(isReachable("192.168.0.200", 8080)); // Always connected to the LAN. assertFalse(isReachable("192.168.0.222", 8080)); // No such a machine. assertFalse(isReachable("noMachine", 8080)); // No such machine. } </code></pre> <p>The following test is only run when the LAN is <strong>disconnected</strong> from the Internet.</p> <pre><code>@Test(timeout=5600) // ~1867ms per call (reasonable?) public void testNoDNS() { assertFalse(isReachable("otherMachine", 8080)); // No DNS. assertFalse(isReachable("example.com", 80)); // No DNS &amp; no Internet. assertFalse(isReachable("208.77.188.166", 80)); // No Internet. } </code></pre> <p>The following test is only run when the LAN is <strong>connected</strong> to the Internet.</p> <pre><code>@Test(timeout=5600) // ~1867ms per call (reasonable?) public void testHaveDNS() { assertTrue(isReachable("otherMachine", 8080)); // DNS resolves local names. assertTrue(isReachable("example.com", 80)); // DNS available. assertTrue(isReachable("208.77.188.166", 80)); // Internet available. } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload