473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 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 17922

@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 concepts between windows os's, Linux, and the...
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 provide web viewers with both streaming (live) video...
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 understand the different ways to do the same thing in...
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 platform. If I am on the linux box i can install...
5
by: TZESENG | last post by:
DECEMBER 13, 2005 . Editions: N. America | Europe | Asia | Edition Preference News Analysis By Steve Hamm Source: http://www.businessweek.com/technology/content/dec2005/tc20051213_042973.htm...
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 tutors are going to be teaching us JAVA wth bluej...
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 code in Windows all works fine, but If I run it in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.