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?
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.