This is the 7th day of my participation in the August Text Challenge.More challenges in August

A picture is an overview of

DNS & hosts

DNS (Full Name: Domain Name System

The principle of the domain name system itself is more complex, but the function of the domain name system is very simple, is to input a domain name, output an IP, here we only do a simple discussion of DNS

For example:

  1. Enter baidu.com in your browser

  2. The browser needs to know what IP address you are visiting, so the browser will ask the operating system

  3. If the operating system does not know, it will ask telecom, Mobile, Unicom such network operators (hereinafter referred to as ISP)

  4. When you pay, the network operator will tell you the IP address of Baidu.com (assuming that the IP address of Baidu.com is 1.2.3.4) and return it to you

  5. After obtaining the IP address of Baidu.com, the browser establishes a TCP connection with the IP address 1.2.3.4

Refinement process:

  1. When entering the baidu.com address, the browser will first check whether it has cache. If it finds that it has visited Baidu.com before, the browser will directly return the last IP address

  2. If the browser doesn’t have a cache, it goes back to the operating system, and the operating system first checks to see if it has a cache, and if it doesn’t, it goes to the ISP.

  3. At ordinary times, we modify the hosts file, which is actually manually setting the cache for the operating system. For example, we write baidu.com 2.3.4.5 in the hosts. In the future, when we visit the address baidu.com, we will not ask ISP any more. IP address 2.3.4.5 is directly accessed through the Settings in hosts

TCP three-way handshake

TCP: Transmission Control Protocol

Suppose A is the browser and B is the server

  1. A first sends A message to B called SYN(x) (assuming x = 100)

    SYN is short for synchronize. Synchronization is not in “synchronous asynchrony”, but in “synchronizing information”. X here is usually a number, and x and y usually start from 0. We do not discuss the meaning of x and y here, please do your own research if you are interested

  2. ACK(x+1) (101) SYN(y) (assume y = 200)

    ACK stands for acknowledge

  3. A will then reply with the message ACK(y+1) (201) to B

From the above process, the following things can be ensured:

  • A can send A message to B
  • User B can receive the message sent by user A
  • B can also send A message to A
  • A can also receive messages sent by B

After the three-way handshake is successful, a TCP connection is established. After the connection is established, content can be transmitted (usually HTTP content or other content, and the transmission process can be bidirectional rather than unidirectional). After the HTTP content is transmitted, the TCP connection is closed.

TCP waved four times

  1. After the above content is transmitted, A will send A FIN(X).

    Note: A may not send the FIN first, but B may send the FIN first

  2. B reply ACK(x+1)

    Note: x and y are generally not 0 when closed

  3. And then I reply FIN(y).

    Steps 2 and 3 May be interspersed with other things

  4. A reply to an ACK (y + 1)

Finally, A and B close their own TCP connections

Some Q & A

Why do I need to close the TCP connection?

Because keeping the connection open wastes memory and CPU

What is the difference between TCP and UDP?

Eight essay, interview must be memorized

  • TCP is stateful, UDP is stateless
  • TCP is reliable, but UDP is unreliable
  • TCP has low transmission efficiency and UDP has high transmission efficiency

Why can’t TCP’s three-way handshake be reduced to two?

To ensure that the client (end A) can receive the data from the server (end B) (if only twice, it is impossible to determine whether A can receive the data from end B)

Why can’t TCP’s four waves merge the middle two steps into one?

Because there is always other data to send between the two steps (between sending ACK(x+1) and FIN(y)), we need to wait for other data to be sent before sending FIN(y).