The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to check internet connection using java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
How to check internet connection using java Posted: Feb 18, 2016 11:46 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: How to check internet connection using java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Hi friends now we are going to check our pc is having internet connection or not using java code 
  • yes we can check internet connection using java code java.net package providing some useful classes.
  • Using java.net.URL and java.net.URLConnection classes we need to check we can check we have a connection or not.
  • Lets see the program to detect internet connection using java
#1:


  1. package InternetConnections;
  2. import java.net.URL;
  3. import java.net.URLConnection; 

  4. public class CheckInternetConnection{
  5.  
  6. public static void main(String[] args){
  7.   
  8. try 
  9. {
  10.         URL url = new URL("http://www.instanceofjava.com");
  11.  
  12.         URLConnection connection = url.openConnection();
  13.         connection.connect();   
  14.  
  15.         System.out.println("Internet Connected");   
  16.             
  17.  }catch (Exception e){
  18.      
  19. System.out.println("Sorry, No Internet Connection");     
  20.                                                             

  21. }
  22. }

Detect internet connection using java program



#2:
  • By using  getRuntime() method of java.lang.Runtime  we can also test internet connection in java
  • The output will be 0 if internet connection available 1 if not.


how to check internet connection in java



#3:


  1. package InternetConnections;
  2.  
  3. import java.net.NetworkInterface;
  4. import java.net.SocketException;
  5. import java.util.Enumeration;

  6. public class CheckInternetConnection{
  7.  
  8. public static void main(String[] args){
  9.  
  10. Enumeration<NetworkInterface> interfaces = null;
  11.  
  12. try {
  13.            
  14.      interfaces = NetworkInterface.getNetworkInterfaces();
  15.  
  16.   } catch (SocketException e) {
  17.             e.printStackTrace();
  18.  }  
  19.  
  20.  while (interfaces.hasMoreElements()) {  
  21.  
  22.       NetworkInterface nic = interfaces.nextElement();   
  23.       System.out.print("Interface Name : [" + nic.getDisplayName() + "]");  
  24.      
  25. try {
  26.       
  27.  System.out.println(", is connected : [" + nic.isUp() + "]"); 

  28. } catch (SocketException e) {
  29.                 e.printStackTrace();
  30. }  
  31.  
  32. }   

  33. }
  34. }

Output:

  1. Interface Name : [Software Loopback Interface 1], is connected : [true]
  2. Interface Name : [WAN Miniport (SSTP)], is connected : [false]
  3. Interface Name : [WAN Miniport (L2TP)], is connected : [false]
  4. Interface Name : [WAN Miniport (PPTP)], is connected : [false]
  5. Interface Name : [WAN Miniport (PPPOE)], is connected : [false]
  6. Interface Name : [WAN Miniport (IPv6)], is connected : [false]
  7. Interface Name : [WAN Miniport (Network Monitor)], is connected : [false]
  8. Interface Name : [WAN Miniport (IP)], is connected : [false]
  9. Interface Name : [RAS Async Adapter], is connected : [false]
  10. Interface Name : [WAN Miniport (IKEv2)], is connected : [false]
  11. Interface Name : [Realtek PCIe GBE Family Controller], is connected : [true]
  12. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter], is connected : [false]
  13. Interface Name : [Teredo Tunneling Pseudo-Interface], is connected : [false]
  14. Interface Name : [Microsoft Virtual WiFi Miniport Adapter], is connected : [true]
  15. Interface Name : [null], is connected : [false]
  16. Interface Name : [Microsoft Virtual WiFi Miniport Adapter #2], is connected : [false]
  17. Interface Name : [null], is connected : [false]
  18. Interface Name : [Teredo Tunneling Pseudo-Interface], is connected : [false]
  19. Interface Name : [Apple Mobile Device Ethernet], is connected : [false]
  20. Interface Name : [Microsoft 6to4 Adapter], is connected : [true]
  21. Interface Name : [Microsoft ISATAP Adapter #4], is connected : [false]
  22. Interface Name : [Microsoft ISATAP Adapter], is connected : [false]
  23. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Connectify WLAN
  24. LightWeight Filter-0000], is connected : [false]
  25. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Connectify NDIS
  26. LightWeight Filter-0000], is connected : [false]
  27. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-QoS Packet
  28. Scheduler0000], is connected : [false]
  29. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Virtual WiFi Filter
  30. Driver-0000], is connected : [false]
  31. Interface Name : [Realtek PCIe GBE Family Controller-Connectify NDIS LightWeight Filter
  32. 0000], is connected : [false]
  33. Interface Name : [Realtek PCIe GBE Family Controller-QoS Packet Scheduler-0000], is
  34. connected : [false]
  35. Interface Name : [Realtek PCIe GBE Family Controller-WFP LightWeight Filter-0000], is
  36. connected : [false]
  37. Interface Name : [Realtek PCIe GBE Family Controller-Deterministic Network Enhancer
  38. 0000], is connected : [false]
  39. Interface Name : [WAN Miniport (IPv6)-Connectify NDIS LightWeight Filter-0000], is
  40. connected : [false]
  41. Interface Name : [WAN Miniport (IPv6)-Deterministic Network Enhancer-0000], is connected
  42. : [false]
  43. Interface Name : [WAN Miniport (IPv6)-QoS Packet Scheduler-0000], is connected : [false]
  44. Interface Name : [WAN Miniport (IP)-Connectify NDIS LightWeight Filter-0000], is
  45. connected : [false]
  46. Interface Name : [WAN Miniport (IP)-Deterministic Network Enhancer-0000], is connected :
  47. [false]
  48. Interface Name : [WAN Miniport (IP)-QoS Packet Scheduler-0000], is connected : [false]
  49. Interface Name : [WAN Miniport (Network Monitor)-Connectify NDIS LightWeight Filter
  50. 0000], is connected : [false]
  51. Interface Name : [WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000], is
  52. connected : [false]
  53. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Native WiFi Filter
  54. Driver-0000], is connected : [false]
  55. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Deterministic Network
  56. Enhancer-0000], is connected : [false]
  57. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-WFP LightWeight
  58. Filter-0000], is connected : [false]
  59. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Native WiFi Filter Driver-0000], is
  60. connected : [false]
  61. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Deterministic Network Enhancer
  62. 0000], is connected : [false]
  63. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Connectify NDIS LightWeight
  64. Filter-0000], is connected : [false]
  65. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-QoS Packet Scheduler-0000], is
  66. connected : [false]
  67. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-WFP LightWeight Filter-0000], is
  68. connected : [false]
  69. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Connectify WLAN LightWeight
  70. Filter-0000], is connected : [false]

Read: How to check internet connection using java

Topic: Java EE in Practice at Lufthansa Previous Topic   Next Topic Topic: How To Build CRUD REST APIs With Elixir And Phoenix Quick Start

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use