Finding Internet IP instead of network IP

Z

zach.bastick

Hello

I am writing an application that needs to use the actual IP of a local
machine. I have tried multiple methods of finding the IP, but it seems
that each method simply returns an IP of a network interface, which is
not always the internet ip of the computer.

InetAddress in = InetAddress.getLocalHost();
InetAddress[] all = InetAddress.getAllByName(in.getHostName());
for (int i=0; i<all.length; i++) {
System.out.println(" address = " + all);
}

The output of the following code on my machine is:

address = blue-monster/192.168.0.244
address = blue-monster/10.0.3.71

...None of these are internet addresses. How do I find my internet IP?

Thanks a lot

Zach


P.S. info about my connection:
I am connected to the internet through a local network. Another
computer on the network (specifically 10.0.3.251) is my gateway. I can
get an internet ip by viewing whatismyip.com.
 
O

Oscar kind

I am writing an application that needs to use the actual IP of a local
machine. I have tried multiple methods of finding the IP, but it seems
that each method simply returns an IP of a network interface, which is
not always the internet ip of the computer.

InetAddress in = InetAddress.getLocalHost();
InetAddress[] all = InetAddress.getAllByName(in.getHostName());
for (int i=0; i<all.length; i++) {
System.out.println(" address = " + all);
}

The output of the following code on my machine is:

address = blue-monster/192.168.0.244
address = blue-monster/10.0.3.71

..None of these are internet addresses. How do I find my internet IP?


You did. Your computer doesn't know about the internet. It just knows that
if it want to send a packet to another computer on the same network, it
can do it itself. It also knows that if it want to send a packet
elsewhere, it uses a gateway (your router most likely).

So what you want to do is get all addresses of your router, and discard
the loopback address and the address on your network. If you have only one
network, you'll now have one address left: the address you're known with
on the internet.
 
J

Juhan Kundla

(e-mail address removed) wrote:

[...]
The output of the following code on my machine is:

address = blue-monster/192.168.0.244
address = blue-monster/10.0.3.71

..None of these are internet addresses. How do I find my internet IP?

You can't, because "your" Internet address is not really yours. It is
the IP-address assigned to the router network interface, not to your
computer. The router does silently a thing called native address
translation and from your computer point of view, you are not supposed
to know anything about it.

You could either write a program, which goes to router and ask its
public IP address, or you could send a IP-packet to an other machine in
Internet and later ask, what this machine thinks the source address was.
The latter is exactly the case with whatismyip.com. Perhaps you could
write a simple java HTTP-client, which goes to the whatismyip.com and
parses the HTML-page.


Juhan
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top