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 ?
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.
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.
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.
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());
}
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
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
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.
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,