3

I've written a programm a year ago and today I saw that this programm give me some curious output. One kind to detect PC's or notebooks in our network I'm using the InetAddress class. My programm works how it should, until Java 1.8.x runs on the executing machine, than the InetAddress.isReachable() gives me a "TRUE" also if there is no machine on that IP address.

private String ipAddress = "192.168.1.200";
inetAddr = InetAddress.getByName(ipAddress);
inetIsReachable = inetAddr.isReachable(8000);
debug += "   INET is reachable: " + inetIsReachable;
System.out.println(debug);

That is really strange, and I don't know how to detect this error.

Kind regards proto

°°°°°EDIT

I tried this code... running on Java 1.7

  inetAddr = InetAddress.getByName(ipAddress);
  inetIsReachable = inetAddr.isReachable(8000);
// FALSE
  byte[] a = new byte[]{(byte) 192,(byte) 168,1,(byte) 200};
  inetAddr = InetAddress.getByAddress("192.168.1.200", a);
  inetIsReachable = inetAddr.isReachable(8000);
// also FALSE

But a ping on commandline reaches the machine!

°°°°°EDIT-2

Java 1.8

192.168.1.200 - InetAddress true - cmd ping true - there is a machine

192.168.1.202 - InetAddress true - cmd ping false - there is no machine

Java 1.7

192.168.1.200 - InetAddress false - cmd ping true - there is a machine

192.168.1.202 - InetAddress false - cmd ping false- there is no machine

prototype0815
  • 592
  • 2
  • 7
  • 24
  • 2
    To debug this you will need to watch the network traffic using a tool such as Wireshark. – Jim Garrison Jul 14 '16 at 06:48
  • 1
    Have you tried `getByAddress` instead of `getByName`? What do you see when you try a ping on cmd/bash to that address? Do you have any Firewall/AntiVirus running? I once had the issue that an AntiVirus Software was just accepting ssh/telnet on _any_ port of a machine that was not otherwise taken. You could only make connection and the rest was dead but it was giving us headaches because the code was testing for availability that way. Different story here but that is one of the first things I check when there is something "strange" with networking. – Fildor Jul 14 '16 at 06:59
  • 2
    I can reproduce this with `1.7.0_80` (printing `false` after 8 seconds) and `1.8.0_101-ea` (printing `true` immediately with the same code). There was at least one bug fix which might be related (http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7163874), but it has been backported to Java 7u45, so the behaviour should still be the same between 7 and 8. On which operating system are you running the application? And, which JRE versions are you using exactly? – Andreas Fester Jul 14 '16 at 07:43
  • @Andreas Fester I'm running on a Windows7 Prof machine. The backport to Java 7u45 is only effective if the user updates his old java version, isn't it? – prototype0815 Jul 14 '16 at 07:47
  • @Fildor see my EDIT – prototype0815 Jul 14 '16 at 07:47
  • 1
    "But a ping on commandline reaches the machine!" ... Let's recap: 1. Your destination machine ( address 192.168.1.200 ) is not running and the address is not taken by any other device in the network. 2. Java 7's `isReachable` returns false. 3. Java 8 will return true. 4. Ping command on commandline will also return true. Are these statements all correct? – Fildor Jul 14 '16 at 07:52
  • 1
    @prototype0815 Yes - it should be in 7u45 upwards. Since I still observe the different behaviour with 7u80, that bug fix is probably not the root cause for your issue. There were some changes in the `isReachable0` native code which actually does the "ping" - [Inet4AddressImpl.c Java 7](http://code.metager.de/source/xref/openjdk/jdk7/jdk/src/solaris/native/java/net/Inet4AddressImpl.c) versus [Inet4AddressImpl.c Java 8](http://code.metager.de/source/xref/openjdk/jdk8/jdk/src/solaris/native/java/net/Inet4AddressImpl.c) – Andreas Fester Jul 14 '16 at 07:53
  • 2
    `But a ping on commandline reaches the machine!` - but then, there must be an interface enabled on that IP and some operating system with an IP stack which at least replies to the ICMP ECHO request. Why do you think that this machine should not be reachable? (Above you say, `... also if there is no machine on that IP address ...`, but obviously there is one ....) – Andreas Fester Jul 14 '16 at 08:03
  • @Fildor see my EDIT-2 – prototype0815 Jul 14 '16 at 08:26
  • 1
    Hmm, so it boils down to Java 7: always false, machine or not, Java 8 always true. I guess you really have to monitor network traffic. I can only make wild guesses from here on. Might be some network device answering all ICMP requests which are handled differently by JRE verisons in use ... I'll +1 question to attract more viewers. Sure one of the cracks instantly knows what the prob is. – Fildor Jul 14 '16 at 08:31
  • @Fildor thank a lot for your help – prototype0815 Jul 14 '16 at 08:37
  • 1
    Did you try with different timeouts? On my machine, it reproducibly returned `true` for non-existing hosts when the timeout is `3000` or higher, but `false` for timeouts of `2999` or lower… – Holger Jul 14 '16 at 11:09
  • The timeout is at 8 seconds (see above), now I tried 15 seconds... Same result... – prototype0815 Jul 14 '16 at 11:53
  • 1
    Did you notice that I wrote that the threshold on my machine was **three** seconds? – Holger Jul 14 '16 at 12:28
  • oh, now I got it.. xD... – prototype0815 Jul 14 '16 at 12:46
  • no change of the behavior with 2500 mSec... JRE 1.7 always false, JRE 1.8 always true – prototype0815 Jul 14 '16 at 12:52

1 Answers1

1

This looks like bug #JDK-8159410, apparently introduced after 8u73.

assylias
  • 321,522
  • 82
  • 660
  • 783