I am working on a Java project which needs to have an internet connection at all times.
I want my program to keep checking the internet connection at some time intervals (say 5 or 10 seconds) and display a message as soon as no internet connection is detected.
I have tried to use the isReachable method to achieve this functionality, below is the code -
try
{
InetAddress add = InetAddress.getByName("www.google.com");
if(add.isReachable(3000)) System.out.println("Yes");
else System.out.println("No");
}
catch (UnknownHostException e)
{
System.out.println("unkownhostexception");
}
catch (IOException e)
{
System.out.println("IoException");
}
But this code always returns "No". What is the problem with this code?
Thanks