- 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
- package InternetConnections;
- import java.net.URL;
- import java.net.URLConnection;
- public class CheckInternetConnection{
- public static void main(String[] args){
- try
- {
- URL url = new URL("http://www.instanceofjava.com");
- URLConnection connection = url.openConnection();
- connection.connect();
- System.out.println("Internet Connected");
- }catch (Exception e){
- System.out.println("Sorry, No Internet Connection");
- }
- }
- }
#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.
#3:
- package InternetConnections;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.util.Enumeration;
- public class CheckInternetConnection{
- public static void main(String[] args){
- Enumeration<NetworkInterface> interfaces = null;
- try {
- interfaces = NetworkInterface.getNetworkInterfaces();
- } catch (SocketException e) {
- e.printStackTrace();
- }
- while (interfaces.hasMoreElements()) {
- NetworkInterface nic = interfaces.nextElement();
- System.out.print("Interface Name : [" + nic.getDisplayName() + "]");
- try {
- System.out.println(", is connected : [" + nic.isUp() + "]");
- } catch (SocketException e) {
- e.printStackTrace();
- }
- }
- }
- }
Output:
- Interface Name : [Software Loopback Interface 1], is connected : [true]
- Interface Name : [WAN Miniport (SSTP)], is connected : [false]
- Interface Name : [WAN Miniport (L2TP)], is connected : [false]
- Interface Name : [WAN Miniport (PPTP)], is connected : [false]
- Interface Name : [WAN Miniport (PPPOE)], is connected : [false]
- Interface Name : [WAN Miniport (IPv6)], is connected : [false]
- Interface Name : [WAN Miniport (Network Monitor)], is connected : [false]
- Interface Name : [WAN Miniport (IP)], is connected : [false]
- Interface Name : [RAS Async Adapter], is connected : [false]
- Interface Name : [WAN Miniport (IKEv2)], is connected : [false]
- Interface Name : [Realtek PCIe GBE Family Controller], is connected : [true]
- Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter], is connected : [false]
- Interface Name : [Teredo Tunneling Pseudo-Interface], is connected : [false]
- Interface Name : [Microsoft Virtual WiFi Miniport Adapter], is connected : [true]
- Interface Name : [null], is connected : [false]
- Interface Name : [Microsoft Virtual WiFi Miniport Adapter #2], is connected : [false]
- Interface Name : [null], is connected : [false]
- Interface Name : [Teredo Tunneling Pseudo-Interface], is connected : [false]
- Interface Name : [Apple Mobile Device Ethernet], is connected : [false]
- Interface Name : [Microsoft 6to4 Adapter], is connected : [true]
- Interface Name : [Microsoft ISATAP Adapter #4], is connected : [false]
- Interface Name : [Microsoft ISATAP Adapter], is connected : [false]
- Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Connectify WLAN
- LightWeight Filter-0000], is connected : [false]
- Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Connectify NDIS
- LightWeight Filter-0000], is connected : [false]
- Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-QoS Packet
- Scheduler0000], is connected : [false]
- Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Virtual WiFi Filter
- Driver-0000], is connected : [false]
- Interface Name : [Realtek PCIe GBE Family Controller-Connectify NDIS LightWeight Filter
- 0000], is connected : [false]
- Interface Name : [Realtek PCIe GBE Family Controller-QoS Packet Scheduler-0000], is
- connected : [false]
- Interface Name : [Realtek PCIe GBE Family Controller-WFP LightWeight Filter-0000], is
- connected : [false]
- Interface Name : [Realtek PCIe GBE Family Controller-Deterministic Network Enhancer
- 0000], is connected : [false]
- Interface Name : [WAN Miniport (IPv6)-Connectify NDIS LightWeight Filter-0000], is
- connected : [false]
- Interface Name : [WAN Miniport (IPv6)-Deterministic Network Enhancer-0000], is connected
- : [false]
- Interface Name : [WAN Miniport (IPv6)-QoS Packet Scheduler-0000], is connected : [false]
- Interface Name : [WAN Miniport (IP)-Connectify NDIS LightWeight Filter-0000], is
- connected : [false]
- Interface Name : [WAN Miniport (IP)-Deterministic Network Enhancer-0000], is connected :
- [false]
- Interface Name : [WAN Miniport (IP)-QoS Packet Scheduler-0000], is connected : [false]
- Interface Name : [WAN Miniport (Network Monitor)-Connectify NDIS LightWeight Filter
- 0000], is connected : [false]
- Interface Name : [WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000], is
- connected : [false]
- Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Native WiFi Filter
- Driver-0000], is connected : [false]
- Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Deterministic Network
- Enhancer-0000], is connected : [false]
- Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-WFP LightWeight
- Filter-0000], is connected : [false]
- Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Native WiFi Filter Driver-0000], is
- connected : [false]
- Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Deterministic Network Enhancer
- 0000], is connected : [false]
- Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Connectify NDIS LightWeight
- Filter-0000], is connected : [false]
- Interface Name : [Microsoft Virtual WiFi Miniport Adapter-QoS Packet Scheduler-0000], is
- connected : [false]
- Interface Name : [Microsoft Virtual WiFi Miniport Adapter-WFP LightWeight Filter-0000], is
- connected : [false]
- Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Connectify WLAN LightWeight
- Filter-0000], is connected : [false]
Easy demonstration of program.! Great approach.
ReplyDelete