Principles of Network Communication Process


Preface

First throw a few questions, the following will explain the principles of network communication around these issues:

  1. How do we communicate with Taobao server when we enter www.taobao.com in our browser?
  2. How do we communicate when we visit a server in the same LAN?
    First of all, let’s talk about the relationship between local area networks and the Internet. In one sentence, the Internet is actually a huge local area network covering the entire world (of course, the local area network is called a WAN, a metropolitan area network, etc.), which is composed of countless local area networks. **If you want a more detailed understanding, you can read this article: What is the Internet (reproduced) . Above
    :
    Insert picture description here
    The square here represents the host (host A is my local machine), the circle represents the switch, and the triangle represents the router.
    Here, through the ping command ping www.taobao.com, you can see that the ip corresponding to the www.taobao.com domain name is 27.221.197.171 after being resolved by the DNS domain name service. The server is responding. Here it is defined as Taobao server A. This article does not delve into the DNS domain name. Analyze the details, mainly discuss the network communication process and principle of the known target ip.
    Insert picture description here

The first step is to encapsulate the TCP/IP data packet and send the data packet to the routing gateway A

As we can see in the figure, the first step is to encapsulate the TCP/IP data packet and send the data packet to the routing gateway.

1. Encapsulate TCP/IP packets

TCP/IP data sheet packaging process

1.1, HTTP protocol message assembly

Because the browser is used for access, the application layer executes the HTTP protocol. First, the message request header, request body, etc. are assembled according to the http protocol.

1.2, TCP protocol message assembly

The TCP protocol package first needs to set the port. The receiver's HTTP port is 80, and the sender's (local) port is a randomly generated integer between 1024-65535, assumed to be 38080, and then the HTTP packet is embedded in TCP The location of the message data.
Insert picture description here

1.3, IP protocol message assembly

The IP data packet first needs to set both ip and TTL time to live, the sender's IP is 192.168.0.4, and the receiver's IP is 27.221.197.171, and then the TCP data packet is embedded in the data location. The value of TTL represents the number of times a data packet can jump in the gateway router. When TTL=0, the router will discard the data packet to prevent packet loops.
Packet loop refers to that router F hangs up data packets and jumps back and forth between routers A, B, C, and D. If there is no TTL, more and more connections to the Taobao website will block the network.

Insert picture description here

1.4 Encapsulate the Ethernet protocol

Ethernet data packets need to set the mac addresses of both parties, the sender is the mac address mac1 of the host A's network card, and the receiver is the mac address mac4 of the gateway router A.
Then encapsulate the IP protocol message to the data location.
Insert picture description here

There may be two questions. How do you know that this data packet should be sent to gateway router A? How to get the mac address of gateway router A?
First look at the currently known conditions: host AIP (192.168.0.4), host AI gateway (192.168.0.1), host AI subnet mask (255.255.255.0), Taobao server IP (27.221.197.171).

  1. When preparing the Ethernet protocol, it will first use the host AIP and Taobao server IP to do the & operation with the host AI subnet mask respectively, and get the network numbers 192.168.0.0 and 27.221.197.0 respectively, which shows that the host A and Taobao server IP are not there. The same local area network.
  2. At this time, comparing the network number 192.168.0.0 with the routing table in host A, it can be concluded that the ip of the next hop is the IP of router A. At this time, it is also necessary to know the mac of router A to encapsulate the Ethernet frame protocol message.
  3. Similarly, according to the & operation, it can be concluded that router A and host A are in the same LAN. At this time, there is a note A that sends out an ARP broadcast asking what mac address corresponds to 192.168.0.1.

Insert picture description here

  1. ARP broadcast will notify all hosts and routers in the LAN. After receiving the ARP broadcast, it will first determine whether the destination IP address matches its own IP and discard it. If it matches, it will respond with ARP and tell the other party its own mac address, so host A can get it. The mac address of router A is mac4

2. The data packet is sent to the routing gateway A

Convert the encapsulated Ethernet frame protocol to binary, and send it to router A through optical cable high and low voltage conversion (high voltage represents 1, low voltage represents 0)

The second step router A sends to router C

  1. When router A receives the data packet and finds that the mac address matches itself, it receives this packet, and then analyzes the IP protocol and finds that the target IP is not that it needs to continue forwarding.
  2. First, judge whether the target IP is in the same LAN as yourself according to the sub-solution mask. If so, send an ARP request to obtain the target IPmac address, which is obviously not in the unified LAN
  3. Comparing the destination IP with the routing table of router A can determine that the IP of the next hop is 27.221.190.1, that is, router C (the principle of comparing and judging the next hop will not be further studied for the time being)
  4. The IP of router C can obtain the mac address of router C through ARP.
  5. Rewrite the packaged Ethernet packet, modify the source mac to mac4, and the target mac to mac4, and send data packets from router A to router C (there is actually a switch between A and C to complete ARP broadcast forwarding and packet forwarding)

The third step router C sends to router F

In the same way, the second step is to put a video below to make it easier to understand how data packets are forwarded between routers.
In the whole process, the IP address remains unchanged, the MAC address changes every hop, the IP address determines the end point, and the MAC address determines the direction of the next hop

Communication between routers of network communication

The fourth router F to Taobao server A

  1. After router F receives the data packet, it can judge that router F and Taobao server A belong to the same local area network according to the IP, this IP, and subnet mask in the data packet.
  2. Then the end of this connection is on the LAN where router F is located, and at this time, ARP broadcast is sent to obtain the mac address according to the destination IP in the IP packet.
  3. Taobao server A receives the ARP request and finds that the IP matches itself, and ARP replies to router F. The local mac address is mac9
  4. Routing server F repackages the Ethernet protocol and sends the data packet to Taobao server A
  5. After receiving it, Taobao server A parses the IP, TCP, and HTTP protocols layer by layer to obtain the request header, and finally transfers to host A in a similar process.

to sum up

Question 1. How do we communicate with Taobao server when we enter www.taobao.com in the browser? Get the answer, think about question 2. How do we communicate when we access the server in the same LAN? Everyone also knows how to communicate. Yes, if you are in the same local area network, you can directly get the mac address by ARP broadcast, encapsulate the Ethernet frame protocol and convert it to binary, and send it to the target through the optical cable high and low voltage conversion (high voltage represents 1, low voltage represents 0) Host.

Guess you like

Origin blog.csdn.net/yangxiaofei_java/article/details/114082674