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.