Communication between intranet and external network (port mapping principle)

Source: Internet
Author: User

Write the easy to understand, special turn over memo.

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? Renting (requesting) a public IP is a need for money. 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 public IP, which Kind of words, and back to the previous question, if A and B LAN IP Same (192.168.31.11), when they visit the Baidu server at the same time, 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. The following figure is a map of the port mapping for 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. This layer-by-layer port mapping ultimately guarantees 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, the other person writes the client program when (the computer can sisu the net), as long as the specified destination IP address is 128.0.0.123, the port is 8888, through this address, Can find the 192.168.31.248:8080, because the two have set up a good mapping, such as the above image, so that our server can receive data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.