开发者

localhost vs real ip address

开发者 https://www.devze.com 2023-03-25 15:24 出处:网络
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, 19

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.


Additionally to the other answers you can run into mysterious problems with using localhost.

One example: IE on Windows makes a shortcut if you enter http://localhost into the URL bar. That becomes a problem when you are developing a web application and have a local proxy installed (such as Fiddler) to peek into messages passing between the client webpage and the server both running locally - the message will not be captured because it will not pass through the proxy in this case. Which makes it a bit inconvenient to find IE-only bugs in your application... ;-)


From Wikipedia, the pertinent points:

Communicating with the loopback interface in an identical manner as with another computers on the network, but bypassing the local network interface hardware, is useful for the purposes of testing software.

Connecting to locally hosted network services, such as a computer game server, or for other inter-process communications, can be performed through loopback addresses in a highly efficient manner.

http://en.wikipedia.org/wiki/Localhost


The difference is when you set up a listening socket (waiting for a connection) you have to tell it which address to bind to. The same applies for website configuration with all of the common web servers.

Normally you will just bind to any address, which means you can access it via 127.0.0.1 or 192.x.x.x or whatever other IP addresses the machine happens to have, but occasionally you may want to bind to a specific one - e.g. if you have 4 public IPs on one machine you may want to have a specific site on a specific IP.

0

精彩评论

暂无评论...
验证码 换一张
取 消