Communication between the (port mapping principle) within the network and external networks

  First, explain the "network" and "out of network" concept:
  the network: that said local area network, such as the school's local area network, LAN IP address of each computer has a reciprocity within the LAN, it is not repeatable. But within two IP network can have the same local area network.
  External network: the Internet, a local area network connected to the external server via a network or a router, the IP address is unique. That network in all of the computers are connected to this external network IP, in foreign exchange data through this external network IP. In other words, a LAN IP network all the computers are different from each other, but share a common external network IP. (With ipconfig / all found in the machine's IP your IP network; www.iis7.com is seen in IP Internet connection you use, i.e. outside the network).
  In the local area network, each computer can be assigned their own IP, the IP is only valid in the LAN. And if you connect your computer to the Internet, your Internet provider (ISP) server will assign an IP address for you, this is the IP address of your external IP network. Two IP exist, one internal and one outside.
  When you buy a home, two computers, you want to set up a local area network, you connect two computers to use in addition to cable and routers and other equipment, you have two computers set a fixed IP, such as computer A is set to 192.168.1.2, computer B is set to 192.168.1.3, so you can use two IP addresses to access two computers to each other, but the two IP addresses only between two computers valid, invalid external network. So the LAN and WAN assigned IP in IP no correspondence.
  Machine in your network, including the Internet, are issued a request to the gateway, and then use the external network IP INT go online by the gateway (typically a router), after receiving the data, and then distributed to your internal network IP.
  Furthermore, most of us people are using broadband internet and found that LAN IP is 192.168.31.11 A and B, when they are browsing the web access Baidu, Baidu when the server response data, how to distinguish between A or B to it?
Communication between the (port mapping principle) within the network and external networks
the difference between public and private IP-IP

  First, we need to look at what is public and private IP IP?

  Public Address (Public address): responsible for Inter NIC (Internet Network Information Center Internet Information Center). The IP address assigned to the registration and organization Inter NIC filed, the world's only public IP, access the Internet directly through it (direct access to the Internet).

  Private address (Private address): is a non-registered address, specifically for the use of internal organization, plainly, private IP can not directly access.

  And we usually via the Internet operators (Telecom, China Mobile, China Unicom broadband, etc.), the family points out through the router IP is private IP (LAN IP), we may doubt that we can access ah, how will it be a private IP ? hire (application) public IP needs money. Operators bought some public IP, and then divide it by these public IP, one by one, and then distributed to users. This process is somewhat similar, we went to the width of the installation, through the separation of several IP routers, so that several people can be online, of course operators through the process of public IP points out is certainly much more complicated than this. So, we usually use the Internet IP is a private IP, IP is really owned public operator (of course, we can hire a public IP). So, the same as a normal LAN IP LAN IP A and B of the family home, but the final A and B to the Internet (data going out) or through the carrier's public IP, after all, limited public IP resources, an area in which user's very possible (in fact, is one such) is the same public IP, so, back to the previous question, if a and B are the same as the LAN IP (192.168.31.11), when they also visit Baidu server time, Baidu server how to distinguish what is A, B which is it?
Communication between the (port mapping principle) within the network and external networks

Port Mapping

  Next, tell you about what is the port mapping?

  NAT is a port mapping, the mapping of the IP address of a host outside the network it to the network port of the machine, to provide the corresponding service. When a user accesses the IP port of the server automatically maps the request to a corresponding internal LAN machines.

  现在市场上的家庭路由器都具备 NAT 功能,也可以实现端口映射。下图为小米路由器的端口映射设置图:
Communication between the (port mapping principle) within the network and external networks
  

  我们平时经过路由器,通过宽带,最终去到运营商那边,数据是从运营商出去,最终数据是回到运营商那边,运营商再把数据发送到用户的电脑。

  路由器,至少有两个端口:WAN 口和 LAN 口。

  WAN:接外部 IP 地址用,通常指的是出口,转发来自内部 LAN 接口的 IP 数据包,这个口的 IP 是唯一的。

  LAN:接内部 IP 地址用,LAN 内部是交换机。

  这里,我们简化这个过程,我们把运营商当做一个 NAT 设备。
Communication between the (port mapping principle) within the network and external networks
  

  为了方便大家理解,我们把 IP 的转化方向反过来分析(准确来说,公网转局域网)。

  A 电脑的 IP 是局域网 IP(192.168.31.11),这个 IP(192.168.31.11)是从路由器的 lan口分配的。

  当我们上百度的时候,经过路由器的 wan口,进行相应的IP、端口转化:192.168.31.11:80 -> 10.221.0.24:8080,所以,从 wan口出去的地址为:10.221.0.24:8080。
Communication between the (port mapping principle) within the network and external networks
  

  最后,经过运营商,运营商那边会做相应的端口映射(而且是动态端口映射),子网 IP(10.221.0.24:8080)转化为公网 IP(128.0.0.1:8888),通过这个公网 IP 去访问百度服务器。
Communication between the (port mapping principle) within the network and external networks
  

  同理,B 的过程也是一样。通过这样的层层端口映射,最终保证地址(IP + 端口)的唯一性。A 和 B 访问百度服务器,尽管它们的局域网 IP 是一样的,但是最终它们访问百度的地址(IP + 端口)是唯一的,所以,百度服务器回复时,原路返回时能够区分到底给谁回。

  如何让外网能够访问自己写的网络程序(服务器)

  首先,我们需要在运营商那边申请(租用)一个公有 IP (长城宽带一年需要 2000 元左右),假如这个公有 IP 为:128.0.0.123。

  假如,我们写的服务器如下:
Communication between the (port mapping principle) within the network and external networks
  

  Then, find a NAT port mapping equipment, home routers have this feature. Here millet router as an example:
Communication between the (port mapping principle) within the network and external networks
  

  Mapping relationship as follows:
Communication between the (port mapping principle) within the network and external networks
  

  Finally, other people write the client program (the computer can on the external network), as long as the destination IP address 128.0.0.123, port 8888, through this address, you can find 192.168.31.248:8080, because the two have been established good map, as shown above, so that our server will be able to receive data.

Guess you like

Origin blog.51cto.com/14479189/2427714