[Java School Recruitment Interview] Basic Knowledge (6) - Computer Network


foreword

This article mainly introduces the related content of computer network.

"Basic knowledge" is the first part of this column. This blog post is the sixth blog post. If necessary, you can:

  1. Click here to return to the index articles of this column
  2. Click here to return to the previous article "[Java School Recruitment Interview] Basic Knowledge (5) - GC"

1. TCP protocol/UDP protocol

1. 建立TCP连接为什么需要三次握手,两次可不可以,为什么?
insert image description here

The reason for the three-way handshake is to ensure that both parties can send and receive data. In the case of two handshakes, since there is no third confirmation, the following situations may occur:

  • The client sent a connection request, but did not receive a confirmation from the server after waiting for a period of time, so it considered that the connection failed and resent the connection request. But at this time, the first request has expired. If the server receives the first request at this time, it will mistakenly think that this is a new connection request, thus establishing two connections.
  • If the server sends an acknowledgment but does not receive an acknowledgment from the client, the server considers the connection establishment to have failed, but the client still considers the connection to be established successfully. In this way, the server will waste resources, because he thinks that this connection has been successfully established, but in fact it has not been successfully established.

Therefore, through the three-way handshake, it can be ensured that both parties can send and receive data, and the value of Sequence is correctly initialized. The sequence value identifies the sequence number of the message transmitted by both ends to ensure that it will not be out of order. The occurrence of the above problems is avoided.

02. TCP连接首次握手的风险
The server received the client's SYN, and did not receive the ACK confirmation after replying with SYN+ACK. The server will resend SYN + ACK until it times out. Under Linux, the default is to retry 5 times, with intervals of 1s, 2s, 4s, 8s, and 16s. After the fifth sending, wait for 32 seconds until the connection is disconnected after a total of 63 seconds.

Therefore, the server will be exposed to the risk of SYN Flood attack.

SYN Flood attack: It uses SYN packets (synchronous packets) in the TCP protocol to occupy server resources. The attacker sends a large number of SYN packets to the server, but does not complete the handshake process, thereby exhausting the server's resources and causing the server to fail to respond to legitimate requests. This kind of attack is usually carried out through multiple IP addresses or using forged IP addresses, with the purpose of making the server unable to work normally, causing service interruption or crash.

03. 针对SYN Flood攻击的防范措施
Enable SYN Cookie: Under Linux, after the SYN queue is full, tcp_syncookiesthe SYN Cookie will be sent back through parameters. If it is a normal connection, the client will send back the SYN Cookie, and the connection can be established directly even if the queue is full. In this way, a large number of forged connection requests can be filtered out to reduce the impact of attacks.

04. 建立TCP连接之后,如果客户机或服务器出现故障,会发生什么?
TCP connections have a keep-alive mechanism. If the connection is inactive within a period of keep-alive time, it will send a keep-alive probe message to the other party. If no response message is received, it will continue to try until the number of keep-alive probes is reached. If no response is received, it will be interrupted. connect.

05. 断开TCP连接时,客户机TIME_WAIT状态的作用是什么?
The TIME_WAIT state refers to the state of waiting for a period of time after the TCP connection is closed to ensure that all data packets are received or retransmitted before completely closing the connection. Its role is to prevent the subsequent data packets of the closed connection from interfering with the new connection, avoiding the "wrong connection" problem. In the TIME_WAIT state, the connection's port number is still reserved so that any legacy data for the connection can be processed. Generally speaking, the duration of the TIME_WAIT state is twice the MSL (Maximum Segment Lifetime), and the MSL is usually 2 minutes.

06. 断开TCP连接为什么需要四次挥手?
insert image description here

In order to ensure that the data can be transmitted completely.
The reason why four waves are required to disconnect the TCP connection is because TCP is a full-duplex protocol, that is, both parties can send data to each other. When disconnecting, both parties must send a confirmation message to ensure that the other party has received their disconnection request. Therefore, four hand waves are required to complete the disconnection process.

07. 服务器出现大量CLOSE_WAIT状态的TCP连接的原因

  • Reason: The client closed the connection, but the server was busy reading or writing data and did not close the connection in time.

  • Solution:
    1) Check the code: especially the part that releases resources;
    2) Check the configuration: especially the thread configuration that handles the request.

08. TCP/IP模型与OSI七层模型的对比

OSI seven layer model TCP/IP model Function Protocol Data Unit (PDU) corresponding agreement Corresponding equipment
application layer application layer file transfer, email, virtual terminal data HTTP、FTP、SMTP、DNS、DHCP、SNMP、Telnet computer
presentation layer Data formatting, transcoding, data encryption
session layer Establish or disassociate connections with other nodes
transport layer transport layer Provides end-to-end connectivity segment TCP、UDP
Network layer Network layer Routing for packets package IP、ICMP、RIP、OSPF、BGP、IGMP router
data link layer link layer Transmission of addressed frames, error checking frame SLIP、CSLIP、PPP、ARP、RARP、MTU switch, bridge
physical layer Transfer data on physical media in binary form bit ISO2110、IEEE802、IEEE802.2 repeater, hub

09. TCP协议如何实现可靠的传输?
1) Data segmentation and numbering: application data is divided into one or more TCP segments, each segment has a 序号sum 确认号. Receivers use these numbers to acknowledge data received and to request missing data.
2) Checksum: Used by the TCP protocol 校验和to detect whether data has been corrupted or tampered with. If data is found to be corrupted or tampered with, TCP discards the data and asks for a resend.
3) Acknowledgment retransmission mechanism: The sender uses a timeout retransmission mechanism to ensure that the data can reach the receiver. If the sender does not receive an acknowledgment, it retransmits the data.
4) Flow Control: Both the sender and receiver maintain a 拥塞窗口size variable to ensure that the network is not overloaded. If the network is congested, the sender reduces the window size, thereby reducing the amount of data sent.
5) Congestion control: By monitoring the congestion state of the network 动态调整数据传输速度and 数据包发送频率avoiding network congestion and packet loss, the reliability and efficiency of data transmission are guaranteed.

10. 确认重传机制是什么?
After sending a piece of data, a timer is started. If the ACK confirmation message for sending data is not received within this time, the message will be retransmitted. When a certain number of times has not been successful, it will give up and send a reset. Signal.

  • Cumulative confirmation: cumulative confirmation is the confirmation method of the TCP protocol. TCP uses variable-length segments to send data. When retransmitting, the segment data may contain more data than the original segment data, so the datagram Simple confirmation cannot be performed with the segment. TCP uses the flow sequence number to confirm a position in the flow, that is, the sequence number and the confirmation number correspond one-to-one, and the receiver uses the sequence number to reorder the message segments for confirmation 正确接收到的流的最长连续前缀.

  • Timeout retransmission: The timeout calculation is the setting time of the timer, so as to ensure the utilization of network resources and avoid affecting the network transmission efficiency due to the uncertainty of the timer time (RTO).

  • Fast retransmission: The receiver is required to send a repeated confirmation immediately after receiving an out-of-sequence segment (in order to let the sender know that a segment has not reached the other party as soon as possible), instead of waiting for the confirmation when sending data.

11. 流量控制中的滑动窗口协议是什么?
The sliding window protocol is a flow control mechanism used to ensure the reliability of data transmission between sender and receiver.

Its working process is as follows:
1) The sender divides the data into multiple data packets and sends them to the receiver in sequence.
2) After receiving the data packet, the receiver stores it in the buffer and sends an acknowledgment message (ACK) to the sender, indicating that it has been successfully received.
3) After receiving the confirmation message, the sender slides the window forward one position, indicating that a data packet has been successfully sent and is ready to send the next data packet.
4) If the receiver fails to receive the packet, no acknowledgment message will be sent. The sender waits for a while, and if the acknowledgment is not received, resends the packet.
5) The sender's window size depends on the receiver's buffer size. If the receiver's buffer is full, the sender stops sending packets until the receiver empties the buffer.
6) If a packet loss or error occurs during data transmission, the sender will receive a retransmission request from the receiver and resend the lost data packet.
7) If the sender does not receive an acknowledgment message or a retransmission request, it will resend the data packet until the receiver confirms that it has been successfully received.

12. 接收窗口和发送窗口大小一样吗?设置越大越好吗?
接收窗口Generally >= 发送窗口, but not the bigger the better. When the receiving window is large enough, not only will it not have a greater impact on reducing the packet loss rate, but it will increase memory consumption.

13. UDP协议的特点
1) Non-connection-oriented;
2) It does not maintain the connection state, and supports transmitting the same message to multiple clients at the same time;
3) The data packet header is only 8 bytes, and the additional overhead is small;
4) The throughput is only limited by the data Generation rate, transmission rate, transmission rate, and machine performance;
5) Best-effort delivery, reliable delivery is not guaranteed, and complex link state tables do not need to be maintained;
6) Message-oriented, the message information submitted by the application is not split or merge.

14. TCP协议和UDP协议的区别

protocol Is it connection oriented reliability orderliness speed magnitude Applicable scene
TCP yes reliable orderly slow heavyweight Scenarios where the data to be transmitted, such as uploading and downloading files, has high reliability
UDP no Unreliable out of order quick lightweight Scenarios such as video streaming and audio streaming that require fast and smooth data reliability but not very high requirements

2. HTTP protocol

The HTTP protocol (Hypertext Transfer Protocol, Hypertext Transfer Protocol) is a protocol for transmitting hypertext data. It is a client-server protocol where the client interacts with the server by sending HTTP requests and the server responds to client requests by sending HTTP responses. The HTTP protocol is based on the TCP/IP protocol, and the HTTP request and response data are transmitted through the TCP connection. The HTTP protocol is the basis of web applications. Through the HTTP protocol, the browser can obtain various resources on the web server, such as web pages, pictures, videos, audios, and so on.

01. HTTP协议的主要特点
1) Supports the client/server model
2) Simple and fast
3) Flexible, allowing the transmission of any type of data object.
4) No connection, only one request can be processed per connection, and the server will disconnect after processing the request and receiving the response from the client.
5) stateless

02. 一个HTTP请求从发起请求到响应的过程
1) The client and the server TCP port (default port number 80) establish a TCP socket connection;
2) The client sends an HTTP request;
3) The server receives the request and returns an HTTP response;
4) The server releases the TCP connection ( In version 1.0, the close method is used, and the server directly disconnects; in 1.1, the keep alive is used, and the server will keep the connection for a certain period of time, during which the client can continue to send requests); 5) The client parses the HTTP response
content .

03. 在浏览器地址栏输入URL,按下回车键之后经历的流程
1) DNS resolution: The browser queries the DNS cache layer by layer according to the URL, and resolves the IP address corresponding to the domain name. DNS cache from near to far is: browser cache, local cache, router cache, IPS server cache, root domain name server cache, top-level domain name server cache.
2) Establish a TCP connection: Establish a TCP connection (three-way handshake) based on the IP address and the corresponding port number.
3) Send an HTTP request;
4) The server processes the request and returns an HTTP response containing page information;
5) The browser receives the response, parses and renders the page;
6) Disconnects the HTTP request: the browser releases the TCP connection (waves four times).

04. 常见的HTTP状态码

  • 1xx: Instruction information, indicating that the request has been received and continues processing
  • 2xx: Success, indicating that the request has been successfully received, understood, and accepted
    • 200, OK: return information normally
  • 3xx: Redirection, further operations must be performed to complete the request
    • 301, Permanent Redirect: The requested resource is permanently assigned a new url, and the new url should be used in the future
    • 302, Temporary Redirect: The requested resource is temporarily assigned a new url
  • 4xx: client error, the request has a syntax error or the request cannot be fulfilled
    • 400, Bad Request: The client request has a syntax error and cannot be understood by the server
    • 401, Unauthorized: The request is unauthorized, this status code must be used with the WWW-Authenticate header field
    • 403, Forbidden: The server received the request, but refused to provide the service
    • 404, Not Found: The requested resource does not exist
  • 5xx: Server error, the server failed to fulfill a valid request
    • 500, Internal Server Error: An unexpected error occurred on the server
    • 503, Server Unavailable: The server is currently unable to process the client's request, and it may return to normal after a period of time

05. 常用的HTTP请求method
The methods of HTTP requests are GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACEand CONNECT, and the most commonly used ones are POSTand GET.

  • Differences:
    1) HTTP message level: GET puts the request parameters URLin , and POST puts them in the message body (ie Body).
    2) Idempotency: For requests that meet confidentiality, the results of one request and multiple requests are the same. Requests using GET should be idempotent, and requests using POST should not be idempotent.
    3) Other levels: Requests using GET can be cached and stored (because the parameters are in the URL), but POST cannot.

06. Cookie和Session的区别

  • Cookies
    are special information sent by the server to the client and stored on the client in the form of text. When the client requests again, the cookie will be sent back. After the server receives it, it will parse the cookie to generate content corresponding to the client.

  • The mechanism of the Session
    server, the information saved on the server. Parse client requests and manipulate session ids, and save state information as required.

  • the difference

    • Cookie data is stored on the client side, and Session is stored on the server side;
    • Session is more secure than Cookie;
    • If you consider reducing the load on the server, you can use cookies.

07. HTTP和HTTPS的区别
Because HTTP uses clear text to transmit data, it does not have security. HTTPS introduces SSL (Security Socket Layer, Secure Socket Layer) to encrypt and decrypt the transmitted data.

  • SSL:

    • A security protocol that provides security and data integrity for network communications;
    • Located between TCP and the application layer, it is the external API of the operating system. After SSL3.0, it was renamed TLS;
    • Authentication and data encryption are used to ensure the security of network communication and the integrity of data.
  • the difference:

    • HTTPS needs to apply for a certificate from a CA, but HTTP does not;
    • HTTPS ciphertext transmission, HTTP plaintext transmission;
    • Different connection methods, HTTPS uses 443the port by default, and HTTP uses 80the port;
    • HTTPS = HTTP + encryption + authentication + integrity protection, more secure than HTTP.

08. HTTPS传输数据的流程
1) The browser sends the supported encryption algorithm information to the server;
2) The server selects a set of encryption algorithms supported by the browser and sends it back to the browser in the form of a certificate;
3) The browser verifies the validity of the certificate and combines it with the public key of the certificate Encrypted information The encrypted information is sent to the server;
4) The server uses the private key to decrypt the information, verifies the hash, and sends the encrypted response information back to the browser;
5) The browser decrypts the response information, verifies the information, and then encrypts the interactive data.

09. socket
It is an abstraction of the TCP/IP protocol and an interface that the operating system opens to the outside world.

The flow of socket communication:
insert image description here
1) Creating a socket: First, create a socket object, which represents a communication endpoint, and needs to specify the protocol and address family.
2) Binding address: Bind the socket to a specific IP address and port so that other processes can find it.
3) Listening connection: If it is the server side, you need to call the listen function to monitor the connection request from the client.
4) Receive connection: When the client requests a connection, the server will call the accept function to create a new socket object for communicating with the client.
5) Send and receive data: Use the send function to send data, and use the recv function to receive data.
6) Close the connection: After the communication is over, the close function needs to be called to close the socket.

The above is the basic flow of socket communication. In different protocols or application scenarios, there will be some special processing, but the basic process is roughly the same.


postscript

The knowledge points related to computer network are very complex and diverse, bringing together the discoveries, research and experimental results of thousands of sages on the road of pioneering. When we were in school, we needed to study a thick book in one semester. Therefore, it is impossible to copy all the content in the book in detail here, and only list the high-frequency test points in the interview here. If you want to systematically learn computer network-related knowledge, it is recommended to buy a book to read.

Guess you like

Origin blog.csdn.net/Mr_Megamind/article/details/130483447