Skip to content

Instantly share code, notes, and snippets.

@6LYTH3
Created April 7, 2014 04:05
Show Gist options
  • Save 6LYTH3/10014680 to your computer and use it in GitHub Desktop.
Save 6LYTH3/10014680 to your computer and use it in GitHub Desktop.
Check whether PC is connected to Internet programmatically using java
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