By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Java and IP address on linux

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

@see java.net.NetworkInterface#getNetworkInterfaces()
Jul 17 '05 #2

P: n/a
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.
Jul 17 '05 #3

P: n/a
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.


Alternatively (e.g. if the system has DHCP), you could send a request to
a site somewhere else on the internet that will send back the IP address
originating the request. See myipaddress.com for an example of what I mean.
Jul 17 '05 #4

P: n/a
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."

Jul 17 '05 #5

P: n/a
Thanks guys for your answers !
Have a nice day !
gurtd
Jul 17 '05 #6

P: n/a
Rob Jones <no****@dontbother.com> wrote in message news:<c4**********@news.wplus.net>...
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


Um, read the javadoc again. The part that you left out states that the
loopback address is only returned if there is a security manager which
disallows looking up the IP address.

This may explain the behavior on Linux; perhaps Java on Linux by
default uses a security manager that disallows getting the local IP
address? I don't know much about security managers or why/when they
are typically used.
Jul 17 '05 #7

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Karl von Laudermann wrote:

[snip]
Um, read the javadoc again. The part that you left out states that
the loopback address is only returned if there is a security manager
which disallows looking up the IP address.

This may explain the behavior on Linux; perhaps Java on Linux by
default uses a security manager that disallows getting the local IP
address? I don't know much about security managers or why/when they
are typically used.


Hi,
I've been silently reading this thread, and the part I still don't get
about getLocalHost(), is: which address?

My computer has an Ethernet card and a dialup modem. At one time, my
Ethernet card had address 10.32.156.1 (static), address 192.168.0.1
(static), and a DHCP address from my ISP via ADSL. I could also dial
my modem to another ISP and get a fourth (dynamic) IP address.

Which one is mine? Which one should getLocalHost() return? Every
single one of those IP addresses was equally legitimately MY address.

That's (I have to assume) why getNetworkInterfaces() was invented. I
suggest using that.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAbdUOnwjA8LryK2IRAs4IAKCHg5T5g3Nw2uhVEDxh9o c5jqjRggCfT4mI
TIZ/IbatULhUit/6mZgH7Yg=
=n7Ws
-----END PGP SIGNATURE-----
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.