Siva Arunachalam
Siva Arunachalam

Reputation: 7740

Java 5 - Check whether a network interface is up and running.

Java 6 offers isUp() method to check whether a network interface is up and running.

http://docs.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#isUp()

Is there any way to check the same in Java 5?

Upvotes: 2

Views: 1333

Answers (3)

Sam Ginrich
Sam Ginrich

Reputation: 841

Class NetworkInterface has a method getInetAddresses(), delivering an enumeration of InetAddress-es. Assuming each adapter supports IPv4, you'll find an Inet4Address among these. if the interface is down it will deliver "0.0.0.0" as address, by method toString(). If an Adapter is up but not linked (no dynamic address assigned), it will have an address of pattern "169.x.x.x".

Upvotes: 0

user207421
user207421

Reputation: 310860

The best way to see whether any resource is available, from an NIC to a database to a Web server on the other side of the world, or the Moon, is to try to use it. Any other technique is liable to the timing problem that it was up when you tested and down when you used it, or the other way around. And when you try to use it you have to deal with the failure cases anyway, because they are generally checked exceptions: why write the same code twice?

Upvotes: 0

Eugenio Cuevas
Eugenio Cuevas

Reputation: 11078

If you won't mind using an external library, check Sigar

You can get the network interface status, among with stats like bytes received or bytes sent.

The only fallback is that is a C library with a java binding, so you will need the specific version for your architecture at runtime

Upvotes: 1

Related Questions