What happens between the browser and the Web server when we type www.baidu.com in the browser’s address bar and hit Enter? How are communication links established between them? There are five steps as follows:
1. Domain name resolution
First, the browser will resolve the IP address corresponding to the domain name www.baidu.com. The resolution steps are as follows:
1.1. The browser will first search for its own DNS cache data. If there is a corresponding cache, the parsing ends
1.2. Search the DNS cache data of the operating system itself. Similarly, if the corresponding cache is found, the parsing ends; if there is no corresponding cache, the following steps are performed
1.3. Try to read the hosts file in the system and check whether there is a corresponding domain-IP mapping in the file. If yes, the parsing is complete
1.4. The Browser sends a request to the DNS system and sends a domain name resolution request to the locally configured preferred DNS server
2. Establish the TCP connection
After the domain name is resolved in Step 1, the browser sends a TCP connection request (three-way handshake) to port 80 of the Web server through a random port 1024 < port 65535.
First handshake: Establish a connection. The client sends a connection request packet and sets the SYN position to 1 and Sequence Number to X. Then, the client enters the SYN_SEND state and waits for confirmation from the server.
Second handshake: The server receives a SYN packet segment. Context The server should acknowledge the SYN segment received from the client, and set this Acknowledgment Number to X +1(Sequence Number+1). Set the SYN position to 1 and Sequence Number to y. The server puts all the above information into a packet segment (namely, SYN+ACK packet segment) and sends it to the client. At this time, the server enters the SYN_RECV state.
Third handshake: The client receives a SYN+ACK packet from the server. A. Then set this Acknowledgment Number to Y +1 and send an ACK segment to the server. After this segment is sent, both the client and the server enter the ESTABLISHED state and complete the TCP three-way handshake.
3. Send the HTTP request
After the TCP connection is established, the browser sends a request to the Web server, such as GET/user/hello.jsp HTTP/1.1, along with the corresponding request message information.
4. The Web server responds to the request and returns a response packet
After receiving the request, the Web server processes it and responds to the request, for example, HTTP/1.1 200 OK, and returns the response packet.
5. The browser parses the HTML of the response message and requests resources in the HTML code
After the browser obtains the HTML file, it parses the HTML code. When it encounters static resources (such as JS and CSS), it requests the server to download them and displays them on the browser page.