0

I have tried obtaining my computer's IP address in two different ways. The first way was to just Google ip address. The second way was to run the following code in Java:

public class YourIPAddress {

public static void main(String[] args) {

  InetAddress ip;
  try {

    ip = InetAddress.getLocalHost();
    System.out.println(ip);
    System.out.println("Current IP address : " + ip.getHostAddress());

  } catch (UnknownHostException e) {

    e.printStackTrace();

  }

}

}

Google returns 50.90.142.29 whereas Java returns 192.168.0.11. Why this conflict in IP addresses?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Mike Warren
  • 3,796
  • 5
  • 47
  • 99
  • 1
    One is the external IP address of your router. The other is your computer's IP address on your local network. – Oliver Charlesworth May 31 '13 at 01:23
  • Welcome to the wonderful world of NAT - [Network Address Translation](https://en.wikipedia.org/wiki/Network_address_translation), whicho is how almost all residential, and many business networks are set up. – rolfl May 31 '13 at 01:25

1 Answers1

1

192.160.0.11 is the private / internal ip address while 50.90.142.29 is the public / external ip address

Stanley
  • 1,421
  • 5
  • 19
  • 36
  • Which the private IP address would be the address of my router? http://whatismyipaddress.com/private-ip – Mike Warren May 31 '13 at 01:30
  • 1
    the private address on your router is likely 192.168.0.1. 192.168.0.11 is likely the private ip of your computer – Stanley May 31 '13 at 01:39
  • and then another question, about the application of this (the reason why I am asking this question): if you make a program for a user that submits data to your computer, would you (the client) need to know their private or public IP address? If public, how would you, in Java, obtain such information as simply as possible? – Mike Warren May 31 '13 at 02:12
  • EDIT: The answer might be the accepted answer to this question: http://stackoverflow.com/questions/2939218/getting-the-external-ip-address-in-java – Mike Warren May 31 '13 at 02:16
  • 1
    you would need to get the remote address of the client from your servlet, like the best answer here: http://stackoverflow.com/questions/4678797/how-do-i-get-the-remote-address-of-a-client-in-servlet request.getRemoteAddr(); but also check for the proxy cases like that response does – Stanley May 31 '13 at 02:17
  • For the reference of anyone having my question who is clueless on what a servlet is, it is simply a conforming Java Server Class: http://en.wikipedia.org/wiki/Servlet – Mike Warren May 31 '13 at 04:04
  • EDIT: The example they have at that link is a METHOD, not a class. My question is, now, is a ServerSocket a servlet? – Mike Warren May 31 '13 at 07:22