I tried InetAddress.getLocalHost().getHostAddress() to get the users internet address. It's giving me 127.0.1.1 but i'm looking for something more like 192.168.1.75. Any idea how to get the address i'm looking for? Thanks - Tyler
EDIT:
I have ubuntu. Remember that. I exported my program to a jar and ran it on my moms Windows laptop. It game me the correct address. That is 192.168.1.64. Still not the correct one on mine.
This means that you have misconfigured your local resolver in some way.
getLocalHost() is supposed to return the real local IP address, and getLoopbackAddress() returns the loopback address, usually 127.0.0.1 (you say you are getting 127.0.1.1; although that's not impossible, I still assume that's a typo?)
There are several situations that you can identify in the Java code for getLocalHost() that will make it return the loopback address instead of the real address:
The local hostname is set to localhost
String local = impl.getLocalHostName();
// [...]
if (local.equals("localhost")) {
return impl.loopbackAddress();
}
Your code doesn't have permissions to get the local host adddress (it may be an applet or a Java WebStart application without permissions)
} catch (java.lang.SecurityException e) {
return impl.loopbackAddress();
}
In other situations, however, it should throw an UnknownHostException.
If your problem is number 1, then you need to change the host name of your machine to something that resolved back to the IP number of your computer.
If your problem is number 2, then you need to make sure that your code gets the appropriate permission, for example by signing the applet or webstart application.
Instead of InetAddress.getLocalHost(), you can use code such as InetAddressLocalHostUtil.java to find the first non-loopback but site local address of the local host ...
Having said that, depending on what you would like to achieve, you may be better off simply using InetAddress.getLoopbackAddress() - it depends if you need an interface that you can connect to externally, or just want to have an address e.g. for two processes on your system to communicate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With