“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

The article is based on the computer network micro-class of Station B (subtitled without background music version) (gradually updated……) Video sorting.

preface

What happens from entering the URL in the address bar to displaying the page? This is a common interview question. So let’s take a look at what happens from the time you type in the URL to the time you respond.

1. DNS resolution

We use a web browser in the user PC to access the content of the external server, and enter the domain name of the external server in the address bar of the web browser. For example, if you enter www.porttest.com, the DNS client process on the PC sends a DNS query packet containing the domain name www.porttest.com. What is the CORRESPONDING IP address?

A DNS query request packet needs to be encapsulated into a UDP user datagram using UDP at the transport layer. In the source port field of the packet header, select an unused port number from transient port 49151 to 65535 to represent the DNS client process, for example, 49152. The value of the destination port field is set to 53, which is the well-known port number used by the DNS server-side process.

Then the USER PC encapsulates UDP user datagrams into IP datagrams and sends them to the DNS server over the Ethernet.

After receiving the packet, the DNS server decapsulates the UDP user packet.

The destination port number in the UDP header is 53, which indicates that the data payload of the UDP user datagram, namely the DNS query request message, should be delivered to the DNS server process on the local server. The DNS server process parses the CONTENT of the DNS query packet and searches for the CORRESPONDING IP address.

If the local DNS server fails to find the destination IP address, the local DNS sends a DNS query to the root DNS server.

If the root DNS server cannot tell the local DNS server which TOP-LEVEL DNS server to access next, a recursive query is used.

Iterative queries are used if the root DNS server can tell the DNS server which top-level DNS server it needs to access next.

After the root DNS server, top-level DNS server, and authoritative DNS server are selected, the authoritative server informs the local server of the destination IP address, and the local DNS server informs the user of the IP address to be accessed.

The following figure shows how the HIERARCHICAL system of the DNS server resolves the IP address of y.abc.com.

After obtaining the target IP address, the DNS server sends a DNS response packet to the user PC. The DNS response packet contains the domain name www.porttest.com and the CORRESPONDING IP address is 192.168.0.3.

DNS response packets need to be encapsulated into UDP user datagrams using UDP protocols at the transport layer. The original port field in the header is set to the familiar port number 53, indicating that this is a UDP user datagrams sent by the DNS server process. The destination port field is set to 49152. This is the transient port number used by the DNS client process that sends DNS query request packets on the user PC.

The DNS server encapsulates UDP user datagrams into IP datagrams and sends them to the user PC over the Ethernet.

The USER PC decapsulates the UDP user datagram after receiving the datagram. The destination port number in the UDP header is 49452, which indicates that the DNS response packet, the data payload part of the UDP user datagram, should be delivered to the DNS client process on the user PC. After the DNS client parses the DNS response packet, it knows that the domain name of the external server it requested is 192.168.0.3.

2. TCP connection

After obtaining the real IP address of the Web server through DNS, the browser on the PC sends a TCP connection request to the Web server. After the connection is established through TCP three-way handshake, the browser can send the HTTP request data to the Web server.

3. Initiate an HTTP request

The HTTP client process on the user’S PC can now send HTTP request packets to the Web server. What is the content of the home page? HTTP request packets need to be encapsulated by TCP at the transport layer into TCP packet segments. The source port field in the header of the PACKET selects an unused port from port 49151 to 65535 to represent the HTTP client process.

For example, the source port still uses 49152, and the destination port field is set to 80, which is the well-known port number used by the HTTP server process. After that, THE TCP packet segment is encapsulated in a datagram and sent to the Web server over Ethernet.

4. Handle requests

After receiving the packet, the Web server decapsulates the TCP packet segment. The destination port number in the TCP header is 80, which indicates that the data payload of the TCP packet segment, namely the HTTP request packet, should be delivered to the HTTP server process on the local server.

The HTTP server process parses the content of the HTTP request packet, searches for the home page content as required, and then sends an HTTP response packet to the user PC containing the home page content requested by the HTTP client.

HTTP response packets need to be encapsulated into TCP packet segments using TCP at the transport layer. The source port number field in the header is set to familiar port number 80, indicating that this is a TCP packet segment sent by the HTTP server process. The destination port field is set to 49152. This is the transient port number used by the HTTP client process that sends HTTP request packets on the user PC.

The Web server encapsulates TCP packets into IP packets and sends them to the user PC over the Ethernet.

5. Browser rendering

After receiving the packet, the user PC decapsulates the TCP packet segment. The destination port number in the TCP header is 49152, which indicates that the data payload of the TCP packet segment, namely, the HTTP response packet, should be delivered to the HTTP client process on the user PC. The HTTP client process parses the CONTENT of the HTTP response packet and displays it in the Web browser.

6. The TCP connection is disconnected

Finally, the client and server terminate the TCP connection by waving four times.

7,

We summarize the above process into a flow chart, as shown below:

Text summary:

(1) DNS resolution: when the user enters a web address and press enter, the browser gets a domain name, and in the actual communication process, we need to be an IP address, so we need to first convert the domain name into the corresponding IP address.

② TCP connection: after the browser obtains the real IP address of the Web server through DNS, it initiates a TCP connection request to the Web server. After the connection is established through TCP three-way handshake, the browser can send the HTTP request data to the server.

③ Send HTTP request: the browser to the Web server to initiate an HTTP request, HTTP protocol is built on the TCP protocol application layer protocol, its essence is in the established TCP connection, according to the HTTP protocol standard send a request for a Web page. In this process, operations such as load balancing are involved.

(4) Processing the request and returning it: After receiving the HTTP request from the client, the server determines how to obtain the corresponding file based on the content in the HTTP request and sends the file to the browser.

⑤ Browser rendering: the browser begins to display the page according to the response, first parsing HTML files to build a DOM tree, and then parsing CSS files to build a rendering tree. When the rendering tree is completed, the browser begins to lay out the rendering tree and draw it to the screen.

⑥ Disconnection: The client and server terminate the TCP connection by waving the hand four times.