0

How can I get the external IP of my computer, solely using Java, without contacting any websites like below:

String ip = "";

    try
    {

        URL whatismyip;

        whatismyip = new URL("http://checkip.amazonaws.com");

        BufferedReader in = null;

        in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));

        ip = in.readLine();

    } catch (UnknownHostException e)
    {
        e.printStackTrace();
    } catch (MalformedURLException e)
    {
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
    }

The reason I do not want this is if this site is down for even a day, I will have big problems.

EDIT:

To define the question a bit clearer: Does Java have any methods to get the user's machine IP, if so, what are they?

Community
  • 1
  • 1
user2344187
  • 13
  • 1
  • 3
  • There are ways to do this using only Java? I saw that to get local IP is simple and easy, but get the external IP (Internet) only in this way? – user2344187 Aug 02 '13 at 18:01
  • "For if one day this site down, I will have big problems." - your question is unclear - are you trying to get the IP of the computer your program is running on ? or do you try to get the IP address of computers that connect to your web-site (server) ? – Nir Alfasi Aug 02 '13 at 18:01
  • "get the external IP of **your** PC" My external IP address is 192.168.0.12 – Steve Kuo Aug 02 '13 at 18:06
  • What OS are you using? I know some Linux-only method that avoids making HTTP request. – tkroman Aug 02 '13 at 18:06
  • @acdcjunior @ Steve P. Do not think it's duplication of questions as mine is to see if there is another way to do this and not only that way. But sorry if duplication is :) – user2344187 Aug 02 '13 at 18:06
  • @user2344187 Yeah, the issue is that there is not other way to do it (to my knowledge), which is why all of the responses are the same. – Steve P. Aug 02 '13 at 18:08
  • @alfasin I trying to get the IP of Internet of the computer your program is running on. – user2344187 Aug 02 '13 at 18:08

1 Answers1

3

Assuming you are in a typical home enviroment (computer - router - internet) using NAT you cannot get that address (something like 83.154.48.7) from a program running in a local machine. The most you can do is get the address of the network adapters installed on it (addresses like 192.0.0.1), since the 'external' address is actually the address of the router.

You need to ask a external site because they will answer to the address of the router (the 83.154.48.7) and the router will send the answer to your pc.

A posibility could be to ask the router for his IP, but viewed from the PC, the router will have an address like 192.0.0.255 for that network. (Dont know if you can get ALL addresses in the router and filter them, though)

For if one day this site down, I will have big problems.

You can have a list of sites where you can get the address from, and try one by one until one of them is up.

Evans
  • 1,589
  • 1
  • 14
  • 25