Joshua
Joshua

Reputation: 26722

What is a lightweight way to monitor a network adapter in Java?

I want to be able to detect when a computer connects to a network. The environment is Java 5 under Windows.

Upvotes: 1

Views: 438

Answers (2)

DaWilli
DaWilli

Reputation: 4898

maybe check in a thread?

try {
  InetAddress inetAddr = InetAddress.getByName(host);
  setOnline();
} catch (UnknownHostException e) {
  setOffline();
}

Upvotes: 0

jsight
jsight

Reputation: 28409

I think your choices are to either use some JNI library (though I'm not familiar with one offhand and that would tend to be platform specific), or just try making a network connection to some known IP (google for example). Neither solution is elegant, but I don't believe there is a pure Java API in 1.5 for determining this.

In Java 1.6, you could use java.net.NetworkInterface.isUp(), but obviously that won't help for your case of using 1.5.

Upvotes: 2

Related Questions