Friday, September 7, 2012

difference between localhost and local ip

What is the difference between the localhost and the real ip address? If I access an application by using http://localhost:8080/index.html and http://192.123.456.001:8080/index.html (for example, 192.123.456.001 is the real ip address of the host computer), what is the difference?
When you access localhost, your /etc/hosts file will tell your computer not to look any further and redirects you to your own computer. When you access the local IP adress, your computer will ask the router to fetch the data, and your router will then point back to your computer.
Is 127.0.0.1 always a different ip address to the machines ip address?
127.0.0.1 is not just a different ip address to the machine ip address, it's a different interface as well. 127.0.0.1 should not be seen on the local network. It's a special internal IP address for the loopback adapter.
x.x.x.x will be your ethernet card.
by the way 'localhost' is simply an entry in your hosts file that points to 127.0.0.1
If you bind a socket to 127.0.0.1:8200 then only clients running locally will be able to access it and only with http://127.0.0.1:8200 or http://localhost:8200 (or a dns or hosts file entry that points to 127.0.0.1).
A socket bound to x.x.x.x:8200 will only be available through http://x.x.x.x:8200 or a dns or hosts file entry that points to that.
Binding a socket to 127.0.0.1 does not automatically make it available on the ethernet interface as well.
Thats why you can't connect to x.x.x.x:8200
You can bind port 8200 to all available interfaces (0.0.0.0) then it should work.
Why is there a difference between ping “localhost” and ping “local IP address”?
You are not pinging the same interface, without any physical interfaces you still have a "local host".
Your localhost is used to refer to your computer from its "internal" IP, not from any "external" IPs of your computer. So, the ping packets don't pass through any physical network interface; only through a virtual loop back interface which directly sends the packets from port to port without any physical hops.
You might still wonder why localhost is resolving to ::1, while traditionally we would expect it to resolve to the IPv4 address 127.0.0.1. Note that .localhost is traditionally a TLD (see RFC 2606) which points back to the loop back IP address (for IPv4, see RFC 3330, especially 127.0.0.0/8).

No comments: