On some Linuxes, when bound to the loopback interface, sendto may fail with an EINVAL errno (see CR 6947677), and on others (AIX, iSeries) it will throw an EHOSTUNREACH errno. In such situation, EHOSTUNREACH should be treated the same as EINVAL, not throw an exception but return false. Below is the simple testcase that can demonstrate this on specific platforms: public class IsReachableTest { public static void main(String[] args) throws Exception{ InetAddress testHost1 = InetAddress.getByName("openjdk.java.net"); NetworkInterface loopbackNi = null; Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); boolean loopbackFound = false; while (list.hasMoreElements() && !loopbackFound){ loopbackNi = list.nextElement(); if (loopbackNi.isLoopback() && loopbackNi.isUp()) { loopbackFound = true; } } if (!loopbackFound) return; if (testHost1.isReachable(loopbackNi, 64, 1000)) System.out.println(String.format("Fail: external host '%s' is reachable via loopback '%s'!", testHost1, loopbackNi)); } } see the mail thread on the net-dev mailing list for further details: http://mail.openjdk.java.net/pipermail/net-dev/2011-December/003839.html
|