KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I would like the fastest and most accurate function boolean isReachable(String host, int port) that passes the following JUnit tests under the conditions below. Timeout values are specified by the JUnit test itself, and may be considered "unreachable." Please note: All answers must be platform-independent. This means that InetAddress.isReachable(int timeout) is not going to work, since it relies on port 7 to do a ping on Windows (ICMP ping being an undocumented function on Windows), and this port is blocked in this setup. LAN Setup: thisMachine ( 192.168.0.100 ) otherMachine ( 192.168.0.200 ) no machine is called noMachine or has the IP 192.168.0.222 (always unreachable) both machines are running Apache Tomcat on port 8080 ; all other ports are unreachable (including port 7 ) example.com ( 208.77.188.166 ) is running a webserver on port 80 and is only reachable when the LAN is connected to the Internet 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). All tests are run on thisMachine . @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()
Tags (comma-separated)
Save Edits
Cancel