Finding an IP address

I

Ike

If I have a client, plugged directly into the internet, say, on a phone
modem, I can get the IP with:

InetAddress.getLocalHost().getHostAddress();

However, if I am also plugged into an ethernet, AND directly into the
internet(again, say, with a phone line) that same call,
InetAddress.getLocalHost().getHostAddress(),
only gets me the IP on the LAN. How can I, under such a circumstance, find
out what the IP is of a given internet connection in such a case? Thanks,
Ike
 
P

Paul Lutus

Ike said:
If I have a client, plugged directly into the internet, say, on a phone
modem, I can get the IP with:

InetAddress.getLocalHost().getHostAddress();

However, if I am also plugged into an ethernet, AND directly into the
internet(again, say, with a phone line) that same call,
InetAddress.getLocalHost().getHostAddress(),
only gets me the IP on the LAN. How can I, under such a circumstance, find
out what the IP is of a given internet connection in such a case?

"Of a given Internet connection"? Does this mean a specific NIC? You really
cannot reliably do this from Java.
 
J

John Davison

Paul said:
Ike wrote:




"Of a given Internet connection"? Does this mean a specific NIC? You really
cannot reliably do this from Java.

Actually you can since 1.4

Enumeration e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface iface = (NetworkInterface)e.nextElement();
System.out.println(iface.getName());
}

You can search by name I guess.

- john
 
T

Tor Iver Wilhelmsen

Ike said:
How can I, under such a circumstance, find out what the IP is of a
given internet connection in such a case? Thanks, Ike

You need to obtain a Socket going to a remote internet address, and
call getLocalAddress().
 
S

Steve Horsley

Ike said:
If I have a client, plugged directly into the internet, say, on a phone
modem, I can get the IP with:

InetAddress.getLocalHost().getHostAddress();

However, if I am also plugged into an ethernet, AND directly into the
internet(again, say, with a phone line) that same call,
InetAddress.getLocalHost().getHostAddress(),
only gets me the IP on the LAN. How can I, under such a circumstance, find
out what the IP is of a given internet connection in such a case? Thanks,
Ike
Well, unless you are behind a box that's doing Network Address Translation,
the LAN IP address will be the address seen on the internet. If you are
going through a NAT box, you have no option but to ask the machine you
are connected to what IP address you APPEAR to have to him.

Steve
 
M

Michael Borgwardt

Steve said:
Well, unless you are behind a box that's doing Network Address Translation,
the LAN IP address will be the address seen on the internet.

Not if you're connected to the internet via a different interface, e.g. a modem.
 
S

Steve Horsley

Michael said:
Not if you're connected to the internet via a different interface, e.g.
a modem.

Urgh. I completely misread the question!
Seems I speed-read and missed a line and a half.

InetAddress.getAllByName(hostname) will retrieve the addresses, but
I suppose you have to guess which is your internet interface's address.

Steve
 
A

Alex Hunsley

Ike said:
If I have a client, plugged directly into the internet, say, on a phone
modem, I can get the IP with:

InetAddress.getLocalHost().getHostAddress();

However, if I am also plugged into an ethernet, AND directly into the
internet(again, say, with a phone line) that same call,
InetAddress.getLocalHost().getHostAddress(),
only gets me the IP on the LAN. How can I, under such a circumstance, find
out what the IP is of a given internet connection in such a case? Thanks,
Ike

The other replies have suggested a few network-techy answers which may
do what you want.
If that fails, one possibility is to have your java code fetch the page:

http://www.whatismyip.com/

....and then parse out the IP displayed from the HTML you get back.
This has the advantage that you will definitely be seeing the IP address
that this website thinks you are at, which will be your external
internet IP, and not a local one by mistake.

alex
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top