472,353 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Java and IP address on linux

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
7 17773

@see java.net.NetworkInterface#getNetworkInterfaces()
Jul 17 '05 #2
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
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
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
Thanks guys for your answers !
Have a nice day !
gurtd
Jul 17 '05 #6
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
-----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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: inquirydog | last post by:
Hello- I, the inquirydog, would like to solicit suggestions for a new web page I am making: I am creating a simple website that will translate...
2
by: Average_Joe | last post by:
Hello Java people, Been awhile since I've used Java, and when I did, it was pretty much XML stuff. Anyhow, I've got a client who needs to...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
7
by: Alex | last post by:
Hi all, I am trying to install a java stored procedure via the windows development centre. The linux box is running 8.1 FP4 as is the windoze...
5
by: TZESENG | last post by:
DECEMBER 13, 2005 . Editions: N. America | Europe | Asia | Edition Preference News Analysis By Steve Hamm Source:...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course...
1
by: jaimemartin | last post by:
hello, I want to validate an xml by means of a schema (xsd). To do that first of all I´m using a SchemaFactory. The problem is that if I run the...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.