How to get my IP address

N

Neutrino

Hello,
I try to read my IP address, under Windows XP, the function
InetAddress.getLocalHost().getHostAddress() return the correct address but
with Linux Red Hat the same function return 127.0.01
How to get the correct address under Linux ?
Thanks
AR
 
M

Michael Borgwardt

Neutrino said:
I try to read my IP address, under Windows XP, the function
InetAddress.getLocalHost().getHostAddress() return the correct address but
with Linux Red Hat the same function return 127.0.01
How to get the correct address under Linux ?

Which is the "correct address"? 127.0.0.1 sounds perfectly correct to me.
One computer can have *a lot* of IP addesses.

You can get all of them via NetworkInterface.getNetworkInterfaces()
and then getInetAddresses() on all of the results.
 
N

Neutrino

Thank you for your answer,
I agree with you, 127.0.01 is correct, but inside my program I have to
connect to a host specifying the local port, so I decided to use the
function : socket (host, remote_port, local, local_port). In a order to get
the local address, I used local = InetAddress.getLocalHost(); this is
working perfectly with Windows XP (java 1.4.2), but with Linux Red Hat, the
connection fails with a Socket Exception "Invalid argument or Cannot assign
requested address". If I change the IP address 127.0.0.1 in the local
InetAddress with the IP address of my interface (I have only one net
interface), the connection is ok. I don't understand why ?
Best regards,
AR
 
G

Gordon Beaton

I agree with you, 127.0.01 is correct, but inside my program I have to
connect to a host specifying the local port, so I decided to use the
function : socket (host, remote_port, local, local_port).

The easiest and most general solution to your problem is to specify
only the local port, not the local address. Here is one way:

Socket s = new Socket(); // unconnected socket!
s.bind(new InetSocketAddress(localPort));
s.connect(new InetSocketAddress(remoteHost, remotePort));
If I change the IP address 127.0.0.1 in the local InetAddress with
the IP address of my interface (I have only one net interface), the
connection is ok. I don't understand why ?

No, you have at least 2 interfaces. lo (with address 127.0.0.1) and
another one, with a different address. You can type "ifconfig -a" to
see all of your interfaces and their addresses.

The local address you choose must be one from which the remote address
is reachable, but 127.0.0.1 isn't such an address (have a look at your
routing table: "netstat -r").

/gordon
 
T

Tim Ward

Neutrino said:
Thank you for your answer,
I agree with you, 127.0.01 is correct, but inside my program I have to
connect to a host specifying the local port, so I decided to use the
function : socket (host, remote_port, local, local_port). In a order to get
the local address, I used local = InetAddress.getLocalHost(); this is
working perfectly with Windows XP (java 1.4.2), but with Linux Red Hat, the
connection fails with a Socket Exception "Invalid argument or Cannot assign
requested address". If I change the IP address 127.0.0.1 in the local
InetAddress with the IP address of my interface (I have only one net
interface), the connection is ok. I don't understand why ?

The world is like that, that's why. Typically your machine will have many IP
addresses, of which 127.0.0.1 is quite often one. Any API that tries to
return "the" IP address of the machine just guesses which one you want, and
different APIs (or even the same API on different operating systems) make
different guesses.

(Even when you managed to work out which is the right one of your IP
addresses you're not finished if you also need the subnet mask - last time I
tried it there appeared to be no (portable) way to get this in Java.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top