0

I'm trying to write a simple program that tests of my internet is working. However, I'm currently getting inaccurate results. No matter what the internet status, I'm getting the result The internet is not available. Help?

Note. This post will be updated as new discoveries emerge, to help those who discover this post in the future.
Thanks a lot to Andreas and Stehen C. for their help in solving this case.
Current status: SOLVED. See Edit 3.


UPDATE: Per Stephen C's comment, Windows may not be able to use this syntax, as Windows "[can't] send ICMP packets". This syntax may work on *nix systems, and OSX. I can't test this.

My original code:

import java.io.IOException;
import java.net.InetAddress;
public class ITIWApp 
{
    public static void main (String [] args) throws IOException
    {
        InetAddress gdns = InetAddress.getByName("8.8.8.8");

        boolean inetIsOn = gdns.isReachable(1000);

        if (!inetIsOn)
        {
            System.out.println("The internet is not available.");
        }
        else
        {
            System.out.println("The internet is available.");
        }
    }
}

EDIT: I have also tried, per @Andreas [link]:

Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
int returnVal = p1.waitFor();
boolean inetIsOn = (returnVal == 0);


if (!inetIsOn)
{
    System.out.println("The internet is not avaliable.");
}
else
{
    System.out.println("The internet is avaliable.");
}

Same result.

EDIT 2: I made this batch file, just to be sure.

@ECHO OFF
echo ITIW?
ping 8.8.8.8
java ITIWApp
pause

The result? Of course I could ping 8.8.8.8 just fine, 0% packet loss, but still got The internet is not available. Which means that:

  • I could reach 8.8.8.8 just fine, and
  • whether or not the Java app is run in Eclipse or command line, it still works for ████.

EDIT 3: On Windows 10, you must be signed in to the super secret Administrator account to use the -c option on ping. To access this account, you must run the command net user administrator /active:yes in a command line. Follow this tutorial here. Thanks to all who helped me in this insanity.

Community
  • 1
  • 1
DigiDuncan
  • 131
  • 1
  • 2
  • 11
  • Is your firewall blocking whatever method [`isReachable`](https://docs.oracle.com/javase/8/docs/api/) uses to determine availability? – Jeffrey Aug 14 '15 at 22:51
  • 1
    Possible duplicate: http://stackoverflow.com/questions/9922543/why-does-inetaddress-isreachable-return-false-when-i-can-ping-the-ip-address – Andreas Aug 14 '15 at 23:02
  • @Andreas That answer isn't working wither. – DigiDuncan Aug 14 '15 at 23:25
  • @Andreas Please see the edited post, new revelations have been made. – DigiDuncan Aug 15 '15 at 16:14
  • @Jeffrey Please see the edited post, new revelations have been made. – DigiDuncan Aug 15 '15 at 16:18
  • Given all the trouble with ICMP from Java and having to run as Admin, I'd suggest a different route. You want to check the connection, right? Why? Ping will only check if the IP stack of the machine is active, but you normally would really want to know if some particular service is available, e.g. HTTP, so you should check for that, not for Ping. If the machine is up, but the web server is down, ping would give you a false positive. For the real answer, you need to check if HTTP is working, and that would not require Admin access. – Andreas Aug 15 '15 at 17:53
  • @DigiDuncan When you end up solving your own problem, the StackOverflow policy is that you create an answer for your own question and then accept that as the right answer. That way others will be able to find the correct answer when searching for the same problem. – Andreas Aug 15 '15 at 17:58

3 Answers3

1

The javadoc for InetAddress.isReachable says:

"Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible.".

And the fact that you apparently can't "ping" the host from outside of Java also supports this. You have a problem with your local networking and / or your ISP.

UPDATE

Actually, it could also be that your Java app doesn't have sufficient access to send ICMP packets. I found this to be the problem when I tried your ITIWApp program on my machine. It said "The internet is unavailable" when I ran it as myself, and "The internet is available" when I ran it with root privilege.

UPDATE 2

Apparently you are trying to run this on Windows. According to this answer, Java on Windows does not support ICMP. It is not a permissions issue on that platform. See also Java bug #8133015


(For what it is worth, I can "ping" 8.8.8.8 from here. But when I try to use "telnet" to 8.8.8.8 on port 7, it times out.)

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Because your IP address is not right. I tried out this code on my machine and it works fine. Cross-check your IP address once again.

agenthost
  • 748
  • 2
  • 9
  • 24
0

On Windows 10, you must be signed in to the super secret Administrator account to use the -c option on ping. To access this account, you must run the command net user administrator /active:yes in a command line. Follow this tutorial here. Thanks to all who helped me in this insanity.

DigiDuncan
  • 131
  • 1
  • 2
  • 11