【Java】Interview frequently asked knowledge points (computer network)

computer network

OSI seven layer model

 

Application layer (Application) :

An interface between a web service and an end user.

The protocols are: HTTP FTP TFTP SMTP SNMP DNS

Presentation Layer:

Data representation, security, compression. (It has been merged into the application layer in the five-layer model)

Formats include JPEG, ASCll, DECOIC, encrypted formats, etc.

Session Layer:

Establish, manage, terminate sessions. (It has been merged into the application layer in the five-layer model)

Corresponding to the host process, it refers to the ongoing session between the local host and the remote host

Transport layer (Transport) :

Define the protocol port number for transmitting data, as well as flow control and error checking.

The protocols are: TCP UDP, once the data packet leaves the network card, it enters the network transport layer

Network layer (Network) :

Perform logical address addressing to realize path selection between different networks.

The protocols are: ICMP IGMP IP(IPV4 IPV6) ARP RARP

Data link layer (Link) :

Functions such as establishing logical connections, performing hardware address addressing, and error checking. (protocol defined by the underlying network)

Combining bits into bytes into frames, using MAC addresses to access media, errors detected but not corrected.

Physical Layer:

Establish, maintain, and disconnect physical connections. (protocol defined by the underlying network)

TCP/IP (five layers)

Presentation layer: solve the data format problem of communication between different hosts

Session layer: Establish and manage communication (connection) between applications (encapsulation of the lower layer) Convenient for users, automatically send and receive packet messages

Transport layer: unpacking and grouping TCP: ensuring the integrity of the data packet and handling possible dangers during transmission UDP: finishing the message after sending it - split into ----> packets

Network layer: routing and address resolution. Select the appropriate network node for routing. package ----> data frame

Data link layer: Controls access to physical devices, specifies how data is transmitted on different physical devices and provides data error correction functions. Provide reliable transmission of data data frames on unreliable physical devices

Physical Layer: Specifications that define the physical devices that communicate. Network cable interface type, optical fiber interface type, transmission rate, etc.

Because Socket communication uses IP and port, these two alone show that it uses the network layer and transport layer; and it ignores the system differences of multiple computer communication, so it involves the presentation layer; generally Socket is based on a Application, so it will involve the session layer and application layer.

http和https

The port of http is 80, the port of https is 443, both are application layer protocols,

HTTPS is set up on SSL/TLS, and will use CA certificates. HTTP response speed is faster than HTTPS, because HTTP uses TCP three-way handshake to establish a connection, and HTTPS sends three packets of TCP, plus nine SSL handshakes. packs, a total of twelve packs.

The workflow is like this:

  1. when requesting https website
  2. The public key and private key will be generated on the server side
  3. Then return the public key to the client
  4. The client verifies whether it is legal. If it is legal, a random key will be generated. If it is not legal, it will respond to https warning
  5. Secretly transmit the randomly generated key to the server
  6. The server will use the private key to decrypt to obtain the key value, and use the key provided by the client to hide the encrypted content
  7. Then send the encrypted content to the client
  8. The client uses a randomly generated key to decrypt

Why is TCP three-way handshake and four-way handshake

The TCP protocol requires a three-way handshake when establishing a connection, and four handshakes when releasing the connection. This is because the design goal of the TCP protocol is to ensure reliability and prevent data loss or errors.

In the process of three-way handshake, the first handshake is that the client sends a SYN message to the server, and the server returns a SYN+ACK message, so that the server knows the client's ability to receive and the willingness to request a connection; the second handshake is the server Send an ACK message to the client and confirm the client's request, so that the client confirms the server's ability to receive and the willingness to request a connection; the third handshake is that the client sends an ACK message to the server again, completing the three-way handshake. A connection is established. Such a design can ensure that both parties can send and receive data correctly, thereby ensuring the reliability of data transmission.

In the process of waving four times, the first waving is when the client sends a FIN message, indicating that it wants to close the connection, but the client may still have unsent data, so it needs to wait for the ACK confirmation from the server; the second waving The server sends an ACK message to confirm the client's close request, and informs the client that it will enter the CLOSE_WAIT state; the third wave is the server sending a FIN message, indicating that the server is also preparing to close the connection, but there may be unsent data, so It still needs to wait for the client's ACK confirmation; the fourth wave is the client sending an ACK message to confirm the server's close request, indicating that both parties have closed the connection. Such a design can ensure that data can be completely transmitted, and at the same time avoid packet loss due to network congestion and other reasons.

To sum up, the TCP protocol uses three-way handshake and four-way handshake to establish and release connections, in order to ensure the reliability and security of data transmission, thereby ensuring the correctness of data.

Main features of TCP and UDP

The main feature of UDP is that UDP is connectionless and does not need to establish a connection before sending data, thus reducing overhead and delay before sending data. UDP uses best-effort delivery, i.e. reliable delivery is not guaranteed, so the host does not need to maintain complex connection state tables. UDP is packet-oriented. UDP neither merges nor splits the packets delivered by the application layer, but preserves the boundaries of these packets. UDP delivers complete packets one at a time. UDP has no congestion control, so network congestion does not slow down the sending rate of the source host. This is important for some real-time applications. It is very suitable for the requirements of multimedia communication. UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communications. The header overhead of UDP is small, only 8 bytes, which is shorter than the 20-byte header of TCP.

TCP is a connection-oriented transport layer protocol.

Each TCP connection can only have two endpoints (endpoint), and each TCP connection can only be point-to-point (one-to-one).

TCP provides a reliably delivered service.

TCP provides full-duplex communication.

stream-oriented

        1. "Stream" in TCP refers to a sequence of bytes flowing into or out of a process.

        2. The meaning of "byte stream oriented" is: Although the interaction between the application program and TCP is one data block at a time, TCP regards the data handed over by the application program as just a series of unstructured byte streams.

Why do you need time_wait?

  1. Reliably terminate TCP connections
  2. Ensure that late TCP packets have enough time to be identified and discarded

Specifically:

  1. When the network situation is not good, if the active party does not wait for TIME_WAIT, after closing the previous connection, the active party and the passive party establish a new TCP connection. At this time, the passive party’s retransmission or delayed fin packets will directly affect the new TCP connection. TCP connections
  2. The same network situation is not good and there is no TIME_WAIT waiting, and there is no new connection after the connection is closed. When the passive party retransmits or delays the fin packet, it returns a RST packet to the passive party, which may affect other service connections of the passive party.

The danger of too much Time_wait

A time_wait takes up 4k in size, too much takes up a lot of memory

On a TCP server with high concurrent short connections, when the server finishes processing the active request to close the connection, there will be a large number of connections in the time_wait (timeout waiting) state, and the server needs a socket to maintain each connection, that is, each connection will occupy a file Descriptors, and the use of file descriptors has an upper limit. If the high concurrency continues, some normal connections will fail

The solution is to modify the configuration so that the server can be quickly recycled and reused in the time_wait state. (I haven't done any specific operation) Configuration file: /etc/sysctl.conf;

There is a net.ipv4.tcp_tw_recycle = 1 to enable fast recycling of TIME-WAIT sockets in TCP connections

DNS

Domain Name System (English: Domain Name System, abbreviation: DNS) is a service of the Internet. As a distributed database that maps domain names and IP addresses to each other, it can make it easier for people to access the Internet. DNS uses TCP and UDP port 53. Currently, the length limit for each level of domain name is 63 characters, and the total length of the domain name cannot exceed 253 characters.

DNS resolution process

The client sends a DNS request, what is your IP in qq, and at the same time, it will say hello to the local domain name server (usually the network service is near the computer room)

After receiving the request locally, the server will have a mapping table between domain name and IP. If it exists, it will tell you, if you want to visit qq, then you should visit XX address. If it does not exist, it will ask the superior (root domain server): "Old iron, can you tell me the IP of www.qq.com"

After the root DNS receives the local DNS request, it finds that it is .com , "www.qq.com, this is managed by Brother .com, I will give you its top-level domain address right away, just ask it."

At this time, the local DNS ran to ask the top-level domain name server, "brother, can you tell me the ip address code of www.qq.com", these top-level domain names are responsible for the second-level domain names such as qq.com

The top-level domain name replies: "Keep this in mind, I will give you the address of the authoritative DNS server in the www.qq.com area", it will tell you

The local DNS asks the authoritative DNS server: "Brother, can you tell me what the corresponding IP of www.qq.com is?"

After querying, the authoritative DNS server tells the local DNS the IP address of the response, and the local server returns the IP address to the client, thereby establishing a connection.

status code:

2xx (3 types)

200 OK: Indicates that the request sent from the client to the server is processed normally and returned;

204 No Content: Indicates that the request sent by the client to the client has been successfully processed, but the returned response message does not contain the main part of the entity (no resource can be returned);

206 Patial Content: Indicates that the client has made a range request, and the server has successfully executed this part of the GET request, and the response message contains the entity content of the range specified by Content-Range.

3xx (5 types)

301 Moved Permanently: Permanent redirection, indicating that the requested resource is assigned a new URL, and the changed URL should be used afterwards;

302 Found: Temporary redirection, indicating that the requested resource has been assigned a new URL, and it is hoped that this visit will use the new URL;

The difference between 301 and 302: the former is a permanent move, the latter is a temporary move (the URL may be changed later)

303 See Other: Indicates that the requested resource has been assigned a new URL, and the GET method should be used to obtain the requested resource;

The difference between 302 and 303: the latter clearly indicates that the client should use GET to obtain resources

304 Not Modified: Indicates that the client sends additional conditions (meaning that the request message using the GET method contains any of if-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since header), the server allows access to resources, but returns a status code if the request meets the conditions;

307 Temporary Redirect: Temporary redirection, which has the same meaning as 303. 307 will follow the browser standard and will not change from POST to GET; (different browsers may have different situations);

4xx (4 types)

400 Bad Request: Indicates that there is a syntax error in the request message;

401 Unauthorized: Unauthorized, HTTP authentication is required;

403 Forbidden: The server denies the access (there is a problem with access rights)

404 Not Found: Indicates that the requested resource cannot be found on the server. In addition, it can also be used when the server rejects the request but does not want to give the reason for the rejection;

5xx (2 types)

500 Inter Server Error: Indicates that an error occurred when the server executed the request, it may also be a bug in the web application or some temporary error;

503 Server Unavailable: Indicates that the server is temporarily overloaded or is being shut down for maintenance and cannot process requests;

Enter a URL change process

After entering the URL in the browser, it usually goes through the following steps:

  1. DNS resolution: The browser first parses the domain name part of the URL and converts the domain name into the server IP address. This process needs to send a request to the DNS server for domain name resolution, and cache the resolved results to improve the speed.

  2. Establish a TCP connection: The browser will use the HTTP protocol to send a request to the server to establish a TCP connection. When establishing a TCP connection, a three-way handshake is required to ensure the reliability of the connection.

  3. Send HTTP request: Once the TCP connection is established, the browser will send an HTTP request to the server. The request includes request method, path, HTTP version, request header and other information.

  4. The server processes the request and returns an HTTP response: After receiving the request, the server will process it according to the content of the request, and then return an HTTP response. The response includes information such as status line, response header, and response content.

  5. Browser processing response: After the browser receives the response, it will process the response, including parsing HTML code, rendering the page, executing JavaScript scripts, and so on. Among them, the HTML code is parsed into a DOM tree, the CSS code is parsed into a CSSOM tree, and then merged into a Render tree for page layout and drawing.

  6. Disconnect TCP connection: After the page is loaded, the browser will disconnect the TCP connection with the server to release resources.

The above is a typical HTTP request process, which also includes some details and handling of special cases, such as HTTP caching, HTTPS security encryption, etc. There may be some differences in different scenarios. But in general, this process describes the main process of a browser accessing a website.

Guess you like

Origin blog.csdn.net/weixin_46601559/article/details/130381899