Tuesday, November 11, 2008

Checking network connection using Java

At times it is required to check whether the machine is still connected to network or not. One way to find it using JDK1.6 by using the code is as below.
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    NetworkInterface nic = interfaces.nextElement();
    System.out.print("Interface Name : [" + nic.getDisplayName() + "]");
    System.out.println(", Is connected : [" + nic.isUp() + "]");
}
Additionally several new useful methods such as isLoopBack(), isPointToPoint() and many more have been added in JDK 1.6 release. Refer to the Javadocs for more information on NetworkInterface class in java.net package.

3 comments:

  1. Hi Mr.navnit,

    You were introduced by my fren varan who the best can be java guru. I'm willing and eager to learn java. Needs your assistance in my learnings. If u are willing, please proposed me the best beginner resources that i can learn java my self.

    confusion with public void main string

    ReplyDelete
  2. Hi,

    I run your code and bellow is my output:

    From my output below, how can i tel that my machine is connected to a LAN or Internet?

    I am new to Java, your help is highly appreciated.

    run:
    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 : [Intel(R) 82579LM Gigabit Network Connection], Is connected : [true]
    Interface Name : [Intel(R) Centrino(R) Advanced-N 6205], Is connected : [true]
    Interface Name : [Cisco AnyConnect VPN Virtual Miniport Adapter for Windows], Is connected : [false]
    Interface Name : [HUAWEI Mobile Connect - Network Adapter], Is connected : [false]
    Interface Name : [Remote NDIS based Internet Sharing Device], Is connected : [false]
    Interface Name : [HUAWEI Mobile Connect - Network Adapter #2], Is connected : [false]
    Interface Name : [Remote NDIS based Internet Sharing Device #2], Is connected : [false]
    Interface Name : [HUAWEI Mobile Connect - Network Adapter #3], Is connected : [false]
    Interface Name : [HUAWEI Mobile Connect - Network Adapter #4], Is connected : [false]
    Interface Name : [Remote NDIS based Internet Sharing Device #3], Is connected : [false]
    Interface Name : [Microsoft Virtual WiFi Miniport Adapter], Is connected : [true]
    Interface Name : [Remote NDIS based Internet Sharing Device #4], Is connected : [false]
    Interface Name : [Intel(R) 82579LM Gigabit Network Connection-Symantec Endpoint Protection Firewall-0000], Is connected : [false]
    Interface Name : [Intel(R) 82579LM Gigabit Network Connection-QoS Packet Scheduler-0000], Is connected : [false]
    Interface Name : [Intel(R) 82579LM Gigabit Network Connection-WFP LightWeight Filter-0000], Is connected : [false]
    Interface Name : [Intel(R) Centrino(R) Advanced-N 6205-Symantec Endpoint Protection Firewall-0000], Is connected : [false]
    Interface Name : [Intel(R) Centrino(R) Advanced-N 6205-Virtual WiFi Filter Driver-0000], Is connected : [false]
    Interface Name : [Intel(R) Centrino(R) Advanced-N 6205-QoS Packet Scheduler-0000], Is connected : [false]
    Interface Name : [WAN Miniport (IPv6)-Symantec Endpoint Protection Firewall-0000], Is connected : [false]
    Interface Name : [WAN Miniport (IPv6)-QoS Packet Scheduler-0000], Is connected : [false]
    Interface Name : [WAN Miniport (IP)-Symantec Endpoint Protection Firewall-0000], Is connected : [false]
    Interface Name : [WAN Miniport (IP)-QoS Packet Scheduler-0000], Is connected : [false]
    Interface Name : [WAN Miniport (Network Monitor)-Symantec Endpoint Protection Firewall-0000], Is connected : [false]
    Interface Name : [WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000], Is connected : [false]
    Interface Name : [Intel(R) Centrino(R) Advanced-N 6205-Native WiFi Filter Driver-0000], Is connected : [false]
    Interface Name : [Intel(R) Centrino(R) Advanced-N 6205-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-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-Symantec Endpoint Protection Firewall-0000], Is connected : [false]
    BUILD SUCCESSFUL (total time: 0 seconds)

    ReplyDelete
    Replies
    1. you only the loop back address "is connected " so you can state that you are not "LAN connected"

      Delete