IP 127.0.0.1 0.0.0.0

The difference between local ip, 127.0.0.1 and 0.0.0.0
 
web java
IP address notation:
An IP address consists of four bytes. In order to facilitate reading and writing, each byte is represented by a number from 0 to 255, and the bytes are separated by '.', such as:
10.10.152.235
Sometimes we see IPs like this:
10.10.152.235/24,
The following /24 means the subnet mask, and 24 means that there are 24 1s on the subnet mask, which is equivalent to 255.255.255.0.
The IP address and the subnet mask are bitwise ANDed to get the Network ID.
The remaining part is the host number in the subnet (host ID, of course, there is a small problem with this name, because now it is not just the host that has an IP, so let's call it that)
 
----------------
Special IP address segment:
127.x.x.x
This is the most familiar local loopback address, which is equivalent to localhost on windows and linux.
We're used to using 127.0.0.1, in fact,
If you ping any address between 127.0.0.1-127.255.255.254 from the command line, the result is the same, they are equivalent.
Normal network packets enter the link layer from the ip layer and are then sent to the network,
The packets sent to the loopback address are directly short-circuited at the IP layer, that is, the packets sent to the IP layer are directly received by the IP layer and are no longer sent downward.
 
Private network address segment:
10.x.x.x、192.168.x.x、172.16.x.x~172.31.x.x、169.254.x.x
These private network address segments are not allowed to appear on the Internet, and the main ones are reserved for the internal networking of enterprises.
This can alleviate the problem of insufficient IP addresses to a certain extent.
The OA network of large enterprises uses 10 address segments more, because this is a class A address segment and contains a lot of IPs.
Small companies use the 192.168.0 address segment more often.
The 169.254 is mainly allocated to the DHCP service.
Reserved address segment:
128.0.x.x、191.255.x.x、192.0.0.x、233.255.255.x
这些地址被保留起来,不做分配且没有明确的用途。
 
其它特殊IP:
255.255.255.255是全局广播地址,
主机号全部为1的地址是子网广播地址,如:192.168.1.255
主机号全部为0的地址是代表该子网的网络地址,如:192.168.1.0
一个非常特殊的IP:0.0.0.0
这个IP相当于java中的this,代表当前设备的IP。
 
我们在java 编程中使用ServerSocket做网络侦听,通常只需要如下代码:
ServerSocket serverSock=new ServerSocket(8888);
serverSock.accept();
假如我的主机ip为:10.10.152.8,
用以上代码做侦听,127.0.0.1:8888或者10.10.152.8:8888都可以连上,
但大家有没有想过过,这个ServerSocket到底使用哪个IP在做侦听?
如果我们将以上代码改成显式绑定:
ServerSocket ss=new ServerSocket();
String ip=“10.10.152.8″;
int port=8888;
InetSocketAddress addr=new InetSocketAddress(ip,port);
ss.bind(addr);
ss.accept();
你会发现,127.0.0.1:8888是无法访问的,
而如果将ip改成127.0.0.1,那么10.10.152.8:8888是无法访问的。
实际上,背后的秘密就在与0.0.0.0这个IP,他可以代表本机的所有IP地址,
但这个IP并不是真是存在的,我们ping不通它,如果将ip改成0.0.0.0:
ServerSocket ss=new ServerSocket();
String ip=“0.0.0.0″;
int port=8888;
InetSocketAddress addr=new InetSocketAddress(ip,port);
ss.bind(addr);
ss.accept();
我们会发现,这和默认行为是一样的,127.0.0.1:8888或者10.10.152.8:8888都可以连上

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327018492&siteId=291194637