dual0 wrote:
gu***@hta-bi.bfh.ch (gu************@hta-bi.bfh.ch to send email write without _dauberie) wrote in message news:<92**************************@posting.google. com>... I can't get the correct IP address on Linux...
The following code works properly on Windows and Unix.
But on Linux, I always get the Loopback Address (127.0.0.1)
Here is the code :
//gets the IP via InetAddress class
//works on UNIX and WIN, but not on LINUX !!
InetAddress hostIP = InetAddress.getLocalHost();
String hostIPStr = hostIP.getHostAddress();
Could anyone help ?
Thanks in advance !
dim
This is because you have a line in /etc/hosts like
127.0.0.1 localhost
you need to have
xxx.xxx.xxx.xxx localhost
and xxx.xxx.xxx.xxx is the ip address you binded to the iface you want.
This would be very bad.
You want to have
127.0.0.1 localhost
as that is the loopback address.
IF your machine is 'arthur.somecompany.com'
with an IP address '192.168.120.1'
then the /etc/hosts file should look like
127.0.0.1 localhost
192.168.120.1 arthur.somecompany.com
I seriously recommending not fixing a Java
coding issues by mucking about with your Linux
config files. Not only is it not right (and
risks breaking some other network aware apps
on your linux box) it will also not work anywhere
else (unless the admins of those boxes are also
like minded).
Sorry I dont have a constructive Java solution
for you but it is better that the solution be in
Java.
BTW the javadoc for
InetAddress.getLocalHost();
read "....an InetAddress representing the loopback address is returned."
so it appears that the implementation of getLocalHost() is incorrect
on your UNIX and WIN boxes. The loopback address is nearly allways 127.0.0.1
Had a quick look for a solution for you.
Found this comment
"A host may have several IP addresses and other
hosts may have to use different addresses to reach it. E.g. hosts on
the intranet may have to use 192.168.0.100, but external machines may
have to use 65.123.66.124.
If you have a socket connection to another host you can call
Socket.getLocalAddress() to find out which local address is used for
the socket."