For beginners only, we learn the network programming (such as TCP,UDP programming), we are usually in the LAN for communication testing, sometimes we may think, we now write the intranet network data and external network data is different, how our intranet data is out of the network?
Moreover, most of us are using broadband Internet, the results found that a and B LAN IP are 192.168.31.11, when they are visiting Baidu browsing the Web page, Baidu Server reply data, how to differentiate is to a or B?
the difference between public IP and private IP
First, what do we need to know about public IP and private IP?
Public Address: Is the responsibility of the Inter NIC (Internet Network Information Center Internet Information Center). These IP addresses are assigned to the organization that registers and applies to the Inter NIC, the only public IP worldwide, through which it directly accesses the Internet (direct access to the Internet).
Private address: belongs to the non-registered address, specifically for the internal use of the organization, plainly, private IP cannot be directly connected to the Internet.
And we usually through the operators (telecommunications, mobile, Unicom broadband, etc.) on the Internet, the home face through the router IP is private IP (LAN IP), we may be questioned, we can surf the Internet ah, how can it be private IP? It takes money to rent (apply) a public IP . The operator buys some public IP and then points it out through these public IPs and then assigns it to one user. This process is a bit like , we go to install the width, through the router to separate a few IP, so that several people can access the Internet, of course, the operator through the public IP separation process is certainly more complicated than this. therefore , we usually use the Internet IP is a private IP, the real public IP is the operator (of course, we can rent a public IP). So, a home LAN IP and b home LAN IP is normal, but eventually a and B can surf the internet (data go out) or through the operator's public IP, after all, the public IP resources are limited, this area of the user is likely to use (in fact, this is the same) is the same A public IP, so, back to the previous question, if A and B LAN IP is the same (192.168.31.11), when they also visit the Baidu server, Baidu server How to distinguish which is a, which is B?
Port Mappings
Next, let's introduce what is port mapping?
Port mapping is a kind of NAT, which maps a port of the IP address of the extranet host to a machine in the intranet and provides the corresponding service. When the user accesses this port of the IP, the server automatically maps the request to a machine within the corresponding LAN.
The home routers on the market now have NAT capabilities and can also implement port mappings. Set the diagram for the port mapping of the Xiaomi router:
We usually pass through the router, through the broadband, eventually go to the operator over there, the data is out from the operator, the final data is back to the operator over there, the operator then sends the data to the user's computer.
Router with at least two ports: WAN Port and LAN port.
WAN: External IP address, usually refers to the export, forwarding from the internal LAN interface IP packets, this port IP is unique.
LAN: Connect internal IP address, LAN internal is switch.
Here, we simplify this process, and we treat the operator as a NAT device.
In order to facilitate understanding, we put the IP conversion direction in turn analysis (accurate, public network to local area network).
The IP of a computer is the LAN IP (192.168.31.11), and this IP(192.168.31.11) is assigned from the LAN port of the router.
when we on Baidu, through the WAN port of the router, the corresponding IP, port conversion: 192.168.31.11:80, 10.221.0.24:8080, so, the address from the WAN port is: 10.221.0.24:8080.
Finally, after the operator, the operator will do the corresponding port mapping (and dynamic port mapping), the subnet IP (10.221.0.24:8080) into the public IP (128.0.0.1:8888), through the public IP to access Baidu server.
Similarly, the process of B is the same. Pass Such layers of port mappings, which ultimately guarantee the uniqueness of the address (IP + port). A and B access Baidu Server, although their local area network IP is the same, but eventually they visit Baidu's address (IP + port) is unique, so, Baidu Server reply, the original way back can distinguish the end to who back.
How to enable the extranet to access its own written network program (server)
First, we need to apply for (lease) a public IP (the Great Wall broadband needs about 2000 yuan a year), if the public IP is: 128.0.0.123.
If we write the following server:
Then, find a NAT device for the corresponding port mapping, home routers have this feature. Here is an example of a Xiaomi router:
The mapping relationship is as follows:
Finally, when other people write the client program (the computer can sisu the net), as long as the IP address is 128.0.0.123, the port is 8888, through this address, will be able to find the 192.168.31.248:8080, because the two have established a good mapping, for example, so that our server can receive data.
Communication between intranet and Extranet