title: How to check internet connectivity in Java
body: <p>I am working on a Java project which needs to have an internet connection at all times.</p>
  
  <p>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.</p>
  
  <p>I have tried to use the isReachable method to achieve this functionality, below is the code -</p>
  
  <pre><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");
  }
  </code></pre>
  
  <p>But this code always returns "No". What is the problem with this code? </p>
  
  <p>Thanks </p>