Thoroughly understand the ip address, distinguish localhost, 127.0.0.1 and 0.0.0.0

1. A popular understanding of what an IP address is

We are not unfamiliar with IP addresses, especially in network visits. (Usually visits to domain names such as Baidu's www.baidu.com are essentially visits to the IP address bound to the domain name), then What is the IP address?
First of all, we need to know that the mutual access in the network is actually the data transfer between the two. Just like express delivery, express delivery can only be delivered to you if you know your address information. When accessing the network, you can only send data to your device if you know your address information on the network. Therefore, the IP address is your address information in the network. The difference from your actual address is that your actual address is expressed in words, while your address (IP address) in the network is expressed in binary numbers.

2. The IP address of the public network and the internal network

Generally, we divide the network into a public network (connected to the Internet) and an intranet.

  • Public IP address:
    Generally, the IP address in the public network is called a public address. Inter NIC (Internet Information Center) is responsible for public addresses. These IP addresses are assigned to organizations that register and apply to Inter NIC. The public address is unique in the world, and it is impossible for two identical IP addresses to exist in the public network.
    Scope: All addresses except private addresses are public addresses
  • Intranet IP address:
    Generally, the IP address in the intranet (ie, local area network) is called a private address. A private address is a non-registered address and is used for internal use by the organization. The range of private addresses is as follows:
A类IP地址中:10.0.0.010.255.255.255
B类IP地址中:172.16.0.0172.31.255.255
C类IP地址中:192.168.0.0192.168.255.255

Private addresses are different from public addresses. They are not Internetallocated and are not allowed to appear in Internet. We can't see them on the public network 私有IP地址, and we 公有地址won't use the above three types of addresses. Therefore, the private address cannot be directly connected to the Internet.

And if you want to use a private address to connect to the Internet to access the public network, what should you do? This requires converting the private IP address into a public IP address to connect to the outside world. So, we usually use the router will be equipped with a known NAT(网络地址转换)software, our router will have at least one valid public IP , NAT will turn into our private address of the router public IP network and external Internet connection . In the same way, because the public IP in the router is used to connect to the Internet, the PC in this intranet displays the public IP of the router in the Internet. This not only provides a certain degree of security, but also It can effectively alleviate the exhaustion of the available IP address space. (Like our school or company's intranet generally do this)

Another point is that in the same local area network, the IP address is unique; but in different local area networks, the IP address can appear repeatedly.

Small example: A local area network, there are a host IP address is 192.168.10.128, so there can never be an IP address for other hosts in the LAN A 192.168.10.128. But in LAN B, there can be the IP address of the host 192.168.10.128.
If the 192.168.10.128host in LAN A 192.168.10.128communicates with the host in LAN B , won't they conflict? No, because as mentioned above, they all use the public IP in the router to connect to the external network, and the public IP is the only one in the world.

Third, the difference between localhost, 127.0.0.1 and 0.0.0.0 and the local IP

  • localhost
    localhost is actually a domain name . Generally, windows system points localhost to 127.0.0.1 by default, but localhost is not equal to 127.0.0.1. The IP address pointed to by localhost is configurable
  • 127.0.0.1
    First of all, we must first know a concept, all IP addresses beginning with 127 are all 回环地址(Loop back address), the loopback interface on which it is located is generally understood as a virtual network card, not a real router interface.
    The so-called loopback address, in layman's terms, is that 127the data packet we send to the initial IP address on the host will be received by the sending host itself, and cannot be transmitted at all, and external devices cannot access the machine through the loopback address.

Small note: Normal data packets will IP层enter 链路层and then be sent to the network; while 回环地址sending data packets, 数据包they will be sent directly to the host IP层获取, and there will be no link layer behind them.

And 127.0.0.1as {127}a member of the set, of course it is also a loopback address. It's just that it is 127.0.0.1often configured by default localhost的IP地址. Generally, you will test whether the network equipment on a certain machine (not the internet connected to the outside) is working properly by ping 127.0.0.1, and test yourself.

  • 0.0.0.0
    First of all, 0.0.0.0it cannot be pinged. In the server, 不是一个真实the IP address combined with 0.0.0.0 means all the IPV4 addresses in this machine. 监听0.0.0.0The port, that's it 监听本机中所有IP的端口.

  • The local IP
    本机IPusually only refers to the same LAN, 能同时被外部设备访问和本机访问的那些IP地址(maybe more than one). Such as 127.0.0.1 is generally not regarded as the local IP. The local IP is bound to a specific network interface, such as an Ethernet card, a wireless network card, or a virtual network card of a PPP/PPPoE dial-up network. If you want to work normally, you must bind an address, otherwise other devices will not know how to access it. for example, by ipconfig, find get IPV4地址为: 192.168.50.213.

Four, small example theater (to help understand the local IP, 127.0.0.1 and 0.0.0.0)

Now there are two pcs in the same LAN, pc1 and pc2 respectively. There is a network card on pc1, the IP address is192.168.10.128

  • pc1 in sever listening 127.0.0.1, then pc1 the client (the client) 可以连上127.0.0.1, ; 192.168.10.128连不上and the client pc2 Rom.
  • pc1 sever the listener 192.168.10.128, then the client can pc1 even on 192.168.10.128, 127.0.0.1Rom; and pc2 on the client can connect 192.168.10.128.
  • If the server in pc1 monitors 0.0.0.0, the client in pc1 can connect to 127.0.0.1and 192.168.10.128, and the client in pc2 can connect 192.168.10.128(very correct).

Summary: In addition 0.0.0.0to listening to all ip addresses, that is, receiving requests. 127.0.0.1、192.168.10.128The servers started by other addresses can only listen to the requests sent from the corresponding IP address.

V. Points to note for local deployment of web services

It is best not to set the local listening address of the web service as IPV4an 192.168.10.128address. If the router's assigned address is random, clientthe address assigned by the end may change and it will not be accessible at that time sever.
If you are client、severon the same computer, you can set the sever address to 127.0.0.1this way to prevent access to requests from other computers.
If you client、severare not on the same computer, you can set the address of the server to 0.0.0.0so that you can receive requests from all computers. If the IP address allocation of each computer is fixed, it can also be the above IPV4address.

Guess you like

Origin blog.csdn.net/yangdashi888/article/details/109778915