1

I would like to find out my external IP address.

A very common solution is to set up a BufferedReader pointing to the website http://checkip.amazonaws.com.

Is there a solution however to do this without connecting to an external website, or in other words, how can I find out my external IP address programmatically?

log_0
  • 838
  • 2
  • 7
  • 17

2 Answers2

8

The answer is a little complicated because it depends exactly how your computer is connected to the internet. If the computer is directly allocated a publicly routeable IP address then you can just list the network addresses assigned to each of the machine's network interfaces. For servers, this scenario is typical.

However, the computer may also be connected through a Network Address Translation (NAT) layer. In this case, your computer is not directly assigned a publicly routeable IP address; instead it is given a local IP address (such as 10.x.x.x or 192.168.x.x) and then a router (usually the next hop) will map packets from the local address space to the public address space. These setups can vary wildly, so it is recommended to just use a well known external service (like checkip.amazonaws.com which you mention), because that method should work regardless of the intermediate address translation.

George
  • 4,147
  • 24
  • 33
  • 1
    Not sure why anyone down voted this answer to -1, this is a correct explanation. There is no way to find the true external IP if the server is NAT'ted. – kjp Feb 18 '15 at 17:31
1

What George P is saying is correct of course, you have to connect to an external router. There is actually specific IETF protocols defined for this very purpose, like http://en.wikipedia.org/wiki/STUN RFC 5389. I therefore instead recommend you to use the standardized way of doing this, i.e. by using a public STUN server.

As an example, have a look here: http://babbly.googlecode.com/svn/trunk/src/org/babbly/core/net/InetAddresResolver.java

For more info and list of public STUN servers, check here: http://www.voip-info.org/wiki/view/STUN

Community
  • 1
  • 1
Andy
  • 2,469
  • 1
  • 25
  • 25