Created
April 7, 2014 04:05
-
-
Save 6LYTH3/10014680 to your computer and use it in GitHub Desktop.
Check whether PC is connected to Internet programmatically using java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.NetworkInterface; | |
import java.net.SocketException; | |
import java.util.Enumeration; | |
public class InternetConnection { | |
public static void main(String[] args) throws InterruptedException { | |
Enumeration<NetworkInterface> interfaces = null; | |
try { | |
interfaces = NetworkInterface.getNetworkInterfaces(); | |
} catch (SocketException e) { | |
e.printStackTrace(); | |
} | |
while (interfaces.hasMoreElements()) { | |
NetworkInterface nif = interfaces.nextElement(); | |
System.out.print("Interface Name : [" + nif.getDisplayName() + "]"); | |
try { | |
System.out.println(", Is connected : [" + nif.isUp() + "]"); | |
} catch (SocketException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
/** | |
* Interface Name : [lxcbr0], Is connected : [true] | |
* Interface Name : [eth0], Is connected : [true] | |
* Interface Name : [lo], Is connected : [true] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment