I believe that many people have encountered such an interview question:

Briefly describe the process of entering a URL(jeujin. Im) from a browserCopy the code

This is a very common question, a random search will yield a large number of answers. The answers are pretty much the same.

  1. Domain name resolution: Resolves a domain name to an IP address, which may go through the browser cache, OS cache, hosts file, and DNS server
  2. After obtaining the IP address, the browser sends an HTTP request
  3. After a three-way handshake, the browser and the server establish a TCP connection and send data
  4. The server receives the data, processes it and returns a response
  5. The browser receives the response result and refreshes the page data

Most of the answers are, but maybe a little bit more detail on each one.

If you want to know more about TCP, you might want to talk about the three-way handshake and the four-way wave. If you want to know more about networking, you might want to talk about the process of sending HTTP requests. Even if he wants to understand the server-side process, he might ask you to describe the relevant Web framework process (SpringMVC process).

This article introduces the role of TCP’s four-tier model through this problem.

The basic information

The configuration of my machine is as follows:

Ipv4 address: 192.168.0.100 Subnet mask: 255.255.255.0 Gateway address: 192.168.0.1 DNS address: 8.8.8.8Copy the code

By default, the reader of this article knows the above concepts in action.

Access to the process

Domain name resolution

First, when you enter the url you want to visit in the browser and press Enter, the first step is to resolve the domain name and obtain the corresponding IP address. This process is also very troublesome, probably check from various caches, if there is no hosts file, continue to go to the DNS server.

Take visiting nuggets (juejin. Im) as an example, this step we can get the corresponding IP(139.198.2.223).

Determine the network segment address

We all know that in computer networks, only machines on the same LAN can be directly connected. If the two machines are not on the same subnet, the gateway (router) is required to communicate.

Therefore, the first step after obtaining the IP address is to determine whether the target IP address and the current host are in the same subnet, and the subnet mask is required to participate in the judgment.

You can determine whether the two IP addresses (destination IP address and local IP address) are in the same network segment by performing logic and operations on the same subnet mask.

And operation is to convert each segment of IP into binary and then calculate the local IP: 192.168.0.100 255.255.255.0 -------------- 192.168.0.0 Digging gold: 139.198.2.223 255.255.255.0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- 139.198.2.0Copy the code

Obviously, different subnets require the gateway to forward data.

Our machine cannot communicate with other machines on the WAN, but the router can, so our requests need to be forwarded by the gateway.

The application layer

Now that we know we need to send a request to the gateway to forward it to the destination address, what data are we sending to the gateway?

Obviously, the browser is sending an HTTP request and sending HTTP request data to the gateway.

An HTTP request packet consists of a request header and a request body.

However, it is not the whole data of the HTTP data packet, only the request body data part. This is what the application layer in the network model does, encapsulating the HTTP request data. The main participants in this layer are the HTTP protocol.

The transport layer

After the application layer, you have the HTTP header and body, and then the request goes to the transport layer, where TCP is involved.

The main task of this layer is to add request ports, both sender and receiver ports. The sender port is a randomly selected free port, while the receiver is usually the default port 80. You can also specify a port in the request. Ports are used to tell the request handler which application is processing the current request data.

In this layer, a TCP header is added to the application layer data. The TCP header contains port information.

The network layer

After passing through the transport layer, there are request packets and request ports at this point. Next, the request goes to the network layer, which is mainly the IP protocol.

The function of the network layer is to add THE IP address to the request packet and encapsulate it into an IP packet. The IP header contains the IP address of the requesting host and the IP address of the destination host.

Data link layer

At this point, the data arrives at the data link layer, and the data packet already includes the request IP, request port and data.

In the data link layer, the MAC addresses of the requester and the target are encapsulated in packets. This layer is attended by the Ethernet protocol.

The Ethernet protocol stipulates that each device connected to the network must have one network adapter (or multiple network adapters), and data packets must be transferred from one network adapter to another. Each nic has a unique identifier, called a MAC address. Ethernet packets must contain the MAC addresses of the sender and the receiver.

The data transfer

Now we have all the data we need and we can start the data transfer.

In this case, transmission does not accurately deliver data to the target machine, but broadcasts the data packet to all machines in the subnet in the form of broadcast. Then the receiving device takes out the MAC address of the receiving end in the data packet and compares it with its own MAC address. If the data is inconsistent, the data is discarded. The process can be continued only when the MAC addresses are consistent.

It is possible to use this step to send data to the destination address if the destination machine is on the same subnet as the sending host. However, if the two subnets are not in the same subnet, the gateway needs to be involved. In this case, the MAC address of the destination address is actually the MAC address of the gateway.

And it may take more than one gateway to send data to the target address at the time of the request.

Request and response

The above process is the process of request, and the process of request processing for the target server is a reverse process, request is to encapsulate the data, and request processing is the process of unpacking the number of packages, each layer to obtain what is needed.

summary

This article is just a very brief introduction to the role of the network model in browser requests. After reading this article, you should have a simple impression, but in reality each layer of processing is cumbersome, and the protocols involved in each layer are also important to learn.