How can i get the LAN IP-address of a computer using Java? I want the IP-address which is connected to the router and the rest of the network.
I\'ve tried something like
This is a method I've used for a while. It includes a little hack to figure out the externally visible ip-address as well.
private List getLocalHostAddresses() {
List addresses = new ArrayList();
try {
Enumeration e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
Enumeration e2 = ni.getInetAddresses();
while (e2.hasMoreElements())
addresses.add(e2.nextElement().getHostAddress());
}
URL u = new URL("http://whatismyip.org");
BufferedReader in = new BufferedReader(new InputStreamReader(
u.openStream()));
addresses.add(in.readLine());
in.close();
} catch (Exception ignore) {
}
return addresses;
}