1

The following snippet :

<%= InetAddress.getLocalHost() %>

gives out this : Feddy/192.168.42.194

but when I check the website ipchicken , I get this :106.193.214.75

Why the two IP differ ?

Y.E.P
  • 1,187
  • 6
  • 20
  • 40

8 Answers8

4

106.193.214.75 is a public IP address of your network.

192.168.42.194 is your local IP address - IP of the machine in internal network. Every machine in your network have the same public IP address.

hsz
  • 148,279
  • 62
  • 259
  • 315
3

An address 192.168.x.x is for private internal networks only. The fact you can talk to the internet means you also have a public IP address.

Its the job of your router to do network address translation so that your devices on your private network all appear with your public address.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

The server is behind NAT which gives it a separate IP address locally compared to the one used on public Internet.

There are several reasons why NAT is used, including security and the limitation of available public IPv4 addresses.

Andreas Åkre Solberg
  • 1,705
  • 12
  • 11
1

192.168.xx.xx is your local ip on your network. 106.193.xxx is your external IP.

You can get both with the following code:

        String hostName = InetAddress.getLocalHost().getHostName();

        InetAddress[] addresses = InetAddress.getAllByName(hostName);
         for (InetAddress a: addresses) {
             System.out.println(a.getHostAddress());
         }
assylias
  • 321,522
  • 82
  • 660
  • 783
  • I get address like : 192.168.42.250 fe80:0:0:0:4c6e:3fff:fe24:75a0%16 – Y.E.P Aug 31 '12 at 12:16
  • the first one is the local IP address and the second one the 'public' ? How to translate it to numeric notation ? – Y.E.P Aug 31 '12 at 12:17
  • @Y.E.P That's an IP v6 whereas you seem to want an IP v4 - You can't really translate from one to the other. [Here is an alternative solution](http://stackoverflow.com/questions/2939218/getting-the-external-ip-address-in-java). – assylias Aug 31 '12 at 12:20
0

One is your local IP address (from your router) and the other one is your IP address over Internet.

192.168 is always from a router

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60
mlemay
  • 1,622
  • 2
  • 32
  • 53
0

The ip 192.168.42.194 is your local ip, it is given to your pc by your router.

The other ip is your WAN ip, it is given by your isp and is the ip address your router gets for connections from the outside world

Minion91
  • 1,911
  • 12
  • 19
0

Because 192.168.42.194 is your private ip, on your private network, and 106.193.214.75 your public one, assigned to your gateway by your ISP.

m4573r
  • 992
  • 7
  • 17
0

In JDK 1.6

List<InetAddress> addrs = new ArrayList<InetAddress>();
for(NetworkInterface ni : NetworkInterface.getNetworkInterfaces()) {
   if(ni.isUp()) {
      for(InetAddress addr : ni.getInetAddresses()) {
        addrs.add(addr);
      }
   }
}

Regards,

Manu Navarro
  • 700
  • 6
  • 9
  • I get address like : `192.168.42.250 fe80:0:0:0:4c6e:3fff:fe24:75a0%16` – Y.E.P Aug 31 '12 at 11:59
  • the first one is the local IP address and the second one the 'public' ? How to translate it to numeric notation ? – Y.E.P Aug 31 '12 at 12:01
  • In this case you only have one IP V4 (192.168.42.250) and one IP V6 (fe80:0:0:0:4c6e:3fff:fe24:75a0%16), it is the same input with the old version and the new – Manu Navarro Aug 31 '12 at 12:04
  • If you do not get no more ip means you have a router on the network this redirects you ip to public ip of your network, the public IP is configured on the router – Manu Navarro Aug 31 '12 at 12:06