[Java Intern] Daily Interview Questions and Check-in-Computer Network

Insert picture description here

1. OSI seven-layer structure, TCP/IP four-layer structure, five-layer protocol structure

  • OSI seven layers : physical layer, data link layer, network layer, transport layer, session layer, presentation layer, application layer.
  • TCP/IP four layers : network interface layer, internet layer, transport layer, application layer.
  • Five-layer protocol : physical layer, data link layer, network layer, transport layer, application layer.

Note : Follow the order from bottom to top.

OSI seven-layer reference model, the role of each layer:
Corresponding layereffectCorresponding network protocol/hardware
Physical layerProvide hardware guarantee for data transmission , network card interface, and transmission medium.Repeaters, hubs, gateways.
data link layerPerform data exchange and convert the data to be transmitted into binary form.Network card, bridge, switch
Network layerCarry out routing and network interconnection.IP , ICMP,...
Transport layerUsed for end-to-end reliable data transmission.TCP , UDP ,...
Session layerIt is used to establish a user-level connection and select the appropriate transmission service .SQL, RPC ,...
Presentation layerIt is used to compress and encrypt data .JPEG, MPEG, ASII,...
Application layerProvide user services, and specific functions are implemented by applications.SMTP, HTTP , DNS,...

2. What is the difference between TCP and UDP? What are the protocols based on TCP and UDP?

What is the difference between TCP and UDP?
  • TCP provides connection-oriented and reliable data stream transmission , while UDP provides connectionless and unreliable data stream transmission .
  • TCP can only communicate one-to-one , while UDP supports one-to-one, one-to-many, many-to-one, and many-to-many communications.
  • The TCP transmission unit is called TCP segment , and the UDP transmission unit is called user datagram .
  • TCP pays attention to data security , UDP data transmission is fast , because there is no need to wait for a connection, many operations are less, but its security is average.
What is the difference between connection-oriented and non-connection-oriented?
  • For connection-oriented services, the communication parties must establish a complete communication channel between the two parties before communicating. During the communication process, the entire connection situation can always be monitored and managed in real time.
  • For non-connection-oriented services, there is no need to establish a connection between two communication nodes in advance. When communication is needed, the sending node can send information to the network so that the information can be transmitted on the network autonomously. Generally, it is not in the process of transmission. Monitor again.
What are the protocols based on TCP and UDP?

TCP-based protocol:

  • HTTP : The transfer protocol used by the Web server to transmit hypertext to the local browser.
  • SMTP : Mail transfer protocol, used to send mail. The server opens port 25 .
  • FTP : defines the file transfer protocol, using port 21 .

UDP-based protocol:

  • DNS : Used for domain name resolution service, which converts domain name address into IP address. DNS uses port 53 .
Applicable scenarios of TCP and UDP:

TCP: When there is a requirement for the quality of network communication, such as: the entire data must be accurately and accurately transmitted to the other party, HTTP, HTTPS, FTP and other file transfer protocols will be used.

UDP: When emphasizing transmission performance rather than transmission integrity, the network communication speed is required to be as fast as possible: such as QQ voice, QQ video, etc.

3. How does the TCP protocol ensure reliable transmission?

TCP uses the following measures to ensure reliable data transmission:
  • ① Divide the application data: Divide the application data into data blocks that TCP considers the most suitable for sending.
  • ② Number the data packets : TCP numbers each data packet to be sent, and the receiver sorts the data packets according to the number, and transmits the ordered data to the application layer.
  • ③ Checksum : This is an end-to-end check, the purpose of which is to detect any changes in data during transmission. If there is an error in the checksum of the receiving end, it means that there is a problem with the data during transmission, and the receiving end will discard the data and no longer accept the data.
  • ④ Flow control : Each party of a TCP connection has a fixed-size buffer space, and the receiving end of TCP only allows the sending end to send data that can be accommodated in the receiving end buffer. When the receiver is too late to process the sender's data, it can prompt the sender to reduce the sending rate to prevent packet loss.The flow control protocol used by TCP is a variable size sliding window protocol. (TCP uses a sliding window to achieve flow control)
  • ⑤ Congestion control : Reduce data transmission when the network is congested. Prevent excessive data from being injected into the network, and avoid overloading the transmission link.
  • ⑥ ARQ protocol : This protocol is also for reliable transmission. Its basic principle is to stop sending every time a packet is sent, and wait for the other party to confirm. After receiving the confirmation, send the next packet.
  • ⑦ Timeout retransmission : When TCP sends a message segment, it starts a timer and waits for the receiving end to confirm receipt of this message segment. If an acknowledgment cannot be received in time, the segment will be resent.
Sliding window and flow control:

TCP uses a sliding window to achieve flow control . Flow control is to control the sending rate of the sender to ensure that the receiver has time to receive.

The window field in the confirmation message sent by the receiver can be used to control the window size of the sender, thereby affecting the sending rate of the sender. If the window field is set to 0, the sender cannot send data.

4. What is TCP sticky packet, its cause and solution?

  • TCP sticky packet means: when several packets sent by the sender are received by the receiver, they will be glued into one packet .
  • TCP is based on byte streams. Although the data exchanges between the application layer and the TCP transport layer are data blocks of varying sizes, TCP treats these data blocks as just a series of unstructured byte streams, without boundaries. May cause byte stream merging (sticky packets).
Reasons for TCP sticky packets:

① Sticky package generated by the sender

  • The client and server that use the TCP protocol to transmit data often maintain a long connection (there is no sticky packet when the data is sent once in a connection), and the two parties can always transmit data when the connection is not disconnected. But when the data packets sent are too small, the TCP protocol will enable the Nagle algorithm by default to combine and send these smaller data packets (buffer data transmission is a process of heap pressure); this combining process is sending buffers In the zone, that is to say, the data is sent out and it is already in the state of sticky packet.
  • Summary: The data to be sent is smaller than the size of the TCP sending buffer. If TCP sends out the data written to the buffer multiple times, sticky packets will occur.

② Sticky package produced by the recipient

  • When the receiver uses the TCP protocol to receive data, the process is like this: the data is sent to the receiver from the bottom of the network model to the transport layer. The TCP protocol processing of the transport layer is to place it in the receiving buffer, and then the application layer actively obtains it. (For example, C language uses functions, etc.); At this time, there will be a problem, that is, the read data function that we call in the program cannot take out the data in the buffer in time, and the next data arrives and part of it is put in The end of the buffer is a sticky packet when we read the data. (The speed of putting data> the speed of taking data in the application layer).
  • Summary: The application layer of the receiving data side does not read the data in the buffer in time, which causes the data in the buffer to accumulate and packet sticking occurs.

How to avoid sticky bags?

There are two ways to avoid sticking packages:

  • A special character is added at the end of each packet to distinguish two consecutive packets. (E.g. \r\ntagging)
  • Add the length of the packet to the header of the message.

5. TCP three-way handshake and four waved hands

  • In order to accurately deliver the data to the destination, the TCP protocol uses a three-way handshake strategy.
  • Establishing a TCP connection requires a "three-way handshake" .

5.1, three-way handshake diagram

As shown in the figure below, the following two robots have confirmed that the other party can correctly receive and send messages through a 3-way handshake:

The process of the above figure is:

  • A handshake: The client sends a data packet with the SYN flag to the server.
  • Two-way handshake: The server sends a data packet with the SYN/ACK flag to the client.
  • Three-way handshake: The client sends a data packet with an ACK flag to the server.

5.2. Why do we need to shake hands three times?

The purpose of the three-way handshake is to establish a reliable communication channel. When it comes to communication, it is simply the sending and receiving of data. The main purpose of the three-way handshake is to confirm that the sending and receiving between themselves and the other party is normal.

  • The first handshake: It is for the server to confirm that the other party (client) sends data normally and receives data normally.
  • The second handshake: It is for the client to confirm that it is normal to send and receive data, and the client also knows that the other party (server) is sending and receiving data normally.
  • The third handshake: It is for the server to confirm that it is sending and receiving data normally, and the server also knows that the other party (client) is sending and receiving data normally.

Therefore, the three-way handshake is indispensable for both the client and the server to know that the sending and receiving functions of themselves and the other party are normal .

5.3. Why does the server send back SYN after the second handshake?

The server sends back the ACK sent by the sender to tell the client that the information I received is indeed the signal you sent, which indicates that the communication from the client to the server is normal. The return SYN is to establish communication from the server to the client.

SYN Synchronize Sequence Numbers (Synchronize Sequence Numbers) are the handshake signals used when TCP/IP establishes a connection. When a normal TCP network connection is established between the client and the server, the client first sends a SYN message, the server uses a SYN-ACK response to indicate that it has received this message, and finally the client responds with an ACK (Acknowledgement) message. In this way, a reliable TCP connection can be established between the client and the server, and data can be transferred between the client and the server.

5.4 Why do you wave your hands four times?

as the picture shows:

Disconnecting a TCP connection requires "waves four times" :

  • First wave: The client sends a FIN packet to the server to tell the server that I want to close the TCP connection with you. (The client actively closes the data transmission to the server, but at this time the client can still receive the data from the server )
  • After the server receives this FIN packet, it returns an ACK packet to the client, confirming that the serial number is the received serial number plus 1 ( same as SYN , a FIN will occupy a serial number), the purpose is to tell the client that I receive It's your request to close the connection.
  • The server actively closes the connection to the client and sends a FIN packet to the client, telling the client that I also closed the TCP connection with you. (The server will no longer send data to the client )
  • After the client receives the FIN packet returned by the server , it sends an ACK packet to the server for confirmation, and sets the confirmation sequence number to the received sequence number plus 1 .

5.5. What if the connection has been established, but the client suddenly fails?

  • TCP has a keep-alive timer . Obviously, if the client fails, the server cannot wait forever, and resources are wasted. The server resets this timer every time it receives a client request. The time is usually set to 2 hours. If it has not received any data from the client for two hours, the server will send a probe segment, which is then sent every 75 seconds once. If there is still no response after sending 10 probe messages, the server considers the client to be faulty, and then closes the connection.

6. Enter the URL address in the browser, and go through those processes after pressing Enter

  • The process experienced : DNS domain name resolution, TCP connection, sending HTTP request, server processing request and returning HTTP message, browser rendering, ending.

Let's analyze it step by step:

① DNS domain name resolution

When we enter a domain name in the browser and press Enter, first check C:\Windows\System32\drivers\etc\hostswhether there is a corresponding domain name mapping under the configuration file of the machine , as shown below:

# 前台本机域名映射 web.csp1999.com
# 后端接口本机域名映射 api.csp1999.com   

If so , return the corresponding IP address.

If not , the browser will send a DNS request to the local DNS server to find the corresponding IP address. The local DNS server is generally provided by your network access server provider, such as China Telecom and China Mobile: (Image source: https:// blog.csdn.net/qq_45173404/article/details/117396953 )

② Establish a TCP connection: three-way handshake

  • After the IP address is resolved , the browser initiates a request to the website server based on the IP address and the default port 443 , and a three-way handshake is performed to establish a TCP connection.

③ Send HTTP request

  • After the TCP connection is established, the browser (client) sends a request through the HTTP protocol to request data from the server.
  • After the server processes the client's HTTP request, it returns the requested data to the browser.

④ Close the TCP connection: wave four times

  • After the data transmission between the browser and the server is completed, a four-way handshake is performed to release the TCP connection.

⑤ Browser echo

  • The browser parses the data and renders it for display.

7. What is the HTTP protocol?

The concept of HTTP:
  • HTTP is a hypertext transfer protocol, used for communication between client and server , that is, "conventions and specifications" for "transmitting" text, pictures, audio, video and other "hypertext" data between "two points".
Features (advantages) of HTTP:
  • The most prominent advantage of HTTP is "simple, flexible and easy to extend, widely used and cross-platform."
Common fields of HTTP:

① Host field

  • When the client sends a request, it is used to specify the domain name of the server.
Host: www.A.com

With Hostfield, you can request to be sent to different sites on the server, "the same."

② Content-Length field

  • When the server returns the data, there will be Content-Lengtha field indicating the data length of this response.
Content-Length: 1000

For example, the above is to tell the browser that the data length of the server response this time is 1000 bytes, and the following bytes belong to the next response.

③ Connection field

  • Connection The field is most commonly used by the client to request the server to use a persistent TCP connection so that other requests can be reused.

The default connection HTTP / 1.1 version is a persistent connection, but for compatibility with older versions of HTTP, you need to specify Connectionthe value of the header field Keep-Alive.

Connection: keep-alive

A reusable TCP connection is established until the client or server actively closes the connection. However, this is not a standard field.

④ Content-Type field

  • Content-Type When the server responds, the field tells the client what format the data is.
Content-Type: text/html; charset=utf-8

The above type indicates that the web page is sent, and the encoding is UTF-8.

When a client requests, you can use Accepta field declaration which data formats they can accept.

Accept: */*

In the above code, the client declares that it can accept data in any format.

⑤ Content-Encoding field

  • Content-EncodingThe field describes the compression method of the data. Indicates what compression format is used for the data returned by the server.
Content-Encoding: gzip

The above indicates that the data returned by the server is compressed using gzip , and the client is informed that it needs to be decompressed in this way.

When the client request, with Accept-Encodingfield specifies the compression method which can accept its own.

Accept-Encoding: gzip, deflate

8. What is the difference between GET and POST?

  • Visibility of parameters in URL : GET parameters are visible (parameters are passed through URL splicing), and POST parameters are not visible.
  • Whether it can be cached : GET requests can be cached, POST requests cannot be cached.
  • The size of the transmitted data : GET general transmission data size does not exceed 2k-4k(Depending on your browser, restrictions are not the same, but the difference is small), POST request to transmit data based on the size of php.inithe configuration file settings can also be infinite.
  • Security : GET is not safe, POST is safe.
  • Number of data packets : GET generates one TCP data packet; POST generates two TCP data packets.

9. The advantages and disadvantages of HTTP

  • The characteristics of the HTTP protocol " stateless, plaintext transmission " are both advantages and disadvantages, and there is also a major disadvantage " insecurity ".

① The advantages and disadvantages of stateless protocols:

  • The benefit of statelessness : The server does not need to remember the HTTP status, and does not require additional resources to record the status information. This can reduce the burden on the server and can use more CPU and memory resources for external services.
  • Disadvantages of statelessness : The server has no memory capability, and it will be very troublesome to complete related operations.
  • For example, login -> add shopping cart -> place an order -> settlement -> payment . This series of operations requires the user's identity to be known. But the server does not know that these requests are related, and must confirm the user identity information every time.

For stateless problems, there are many solutions, among which the simpler method uses Cookie technology.

CookieThe state of the client is controlled by writing cookie information in the request and response. It is equivalent to that after the client's first request, the server will issue a "small sticker" with the client's identity information. When the client subsequently requests the server, bring the "small sticker" and the server can try it :

② Advantages and disadvantages of plaintext transmission:

Plain text means that the information in the transmission process can be easily read. It can be directly viewed with the naked eye through the browser's F12 console or Wireshark capture, which brings great convenience to our debugging work.

But this is exactly this, all HTTP information is exposed in broad daylight, which is equivalent to information streaking . In the long process of transmission, the content of the information has no privacy at all, and it can be easily stolen. If there is your account password information in it, then your account is gone .

10. What is the difference between HTTP and HTTPS?

  • ① Security difference : HTTP is a hypertext transfer protocol, and information is transmitted in plain text, which poses a security risk. HTTPS can ensure the security of information transmission. The SSL/TLS security protocol is added between the TCP and HTTP network layers to enable encrypted transmission of messages.
  • ② Differences in the connection process of Jianli : HTTP connection establishment is relatively simple, and HTTP message transmission can be carried out after the TCP three-way handshake. In HTTPS, after the TCP three-way handshake, the SSL/TLS handshake process is required before it can enter the encrypted message transmission.
  • ③ Port difference : HTTP port number is 80, HTTPS port number is 443.
  • ④ Do you need to apply for a digital certificate : HTTPS protocol needs to apply for a digital certificate from a CA (certificate authority) to ensure that the identity of the server is trustworthy. The HTTP protocol is not required.
Hybrid encryption:

HTTPS uses a "hybrid encryption" method that combines symmetric encryption and asymmetric encryption , and puts the server's public key in a digital certificate to solve the risk of eavesdropping .

  • Before communication is established, asymmetric encryption is used to exchange the "session key", and subsequent asymmetric encryption is no longer used.
  • In the communication process, all plaintext data is encrypted using the "session key" method of symmetric encryption .
Summary algorithm:
  • HTTPS uses a digest algorithm to ensure the integrity of the data and solve the risk of data tampering.
  • The digest algorithm can generate a unique "identification" for the data, which is used to verify the integrity of the data, thereby solving the risk of tampering.
Between HTTP and HTTPS added to the TCP layer SSL/TLSprotocol, as shown:

11. What is the role of cookies? What is the difference between Session and Session?

The role of Cookie and Session:

Cookie and Session are both session methods used to track the identity of the browser user, but there are differences between the two:

Cookies are generally used to save user information . For example, we save the information of users who have logged in in the Cookie, and the page can automatically fill in some basic information for you to log in when you visit the website next time;

**Session's main function is to record the user's state through the server. **A typical scenario is a shopping cart. When you want to add an item to the shopping cart, the system does not know which user operated it, because the HTTP protocol is stateless. After the server creates a specific session for a specific user, it can identify the user and track the user.

The difference between Cookie and Session is as follows:
  • Cookie data is stored on the client side, and Session data is stored on the server side.
  • Cookie is stored in the client, while the session is stored on the server, relatively speaking, the session security is higher. If you want to store some sensitive information in a cookie, do not write it directly into the cookie. It is best to encrypt the cookie information and then go to the server to decrypt it when it is used.
  • Cookies are generally used to store user information, and the main function of the session is to record the user's status through the server.

12. What is the difference between URI and URL?

  • URI : It is a uniform resource identifier that can uniquely identify a resource.
  • For example: such as: https://blog.csdn.net/qq_32595453/article/details/79516787
  • URL : It is a uniform resource locator, which can provide the path of the resource. URL is a subset of URI. It not only uniquely identifies a resource, but also provides information for locating the resource.

The function of the URI is like the ID number, and the function of the URL is more like the home address.

13. What are the main differences between HTTP 1.0, HTTP 1.1 and HTTP 2.0?

HTTP 1.0: The short connection is used by default . The browser needs to establish a TCP connection with the server for each request. The server will disconnect the TCP connection (no connection) immediately after the server is processed. The server does not record the client's request status (stateless).

HTTP 1.1 : Long connection is used by default to maintain connection characteristics. Using the HTTP protocol for long connections, this line of code will be added to the response header:

// Keep-Alive不会永久保持连接,它有一个持续时间,可以在不同的服务器软件中设定这个时间。
// 实现长连接需要客户端和服务端都支持长连接。

In the case of a long connection, when a web page is opened, the TCP connection used to transmit HTTP data between the client and the server will not be closed. When the client accesses the server again, it will continue to use the established connection .

HTTP 2.0: Introduces the concept of binary data frames and streams , supports multiplexing, server push, and supports the use of binary format to transmit data , while HTTP 1.0 still uses text format transmission.

14. Briefly introduce the difference between symmetric encryption algorithm and asymmetric encryption algorithm?

  • Symmetric encryption algorithm : Both parties hold the same key , and the encryption speed is fast . Typical symmetric encryption algorithms: DES, AES.
  • Asymmetric encryption algorithm : The keys appear in pairs ( private key, public key ), the private key is only known by itself and not transmitted on the network; while the public key can be made public. Compared with symmetric encryption, the speed is slower. Typical asymmetric encryption algorithms are: RSA and DSA.

15. How does HTTPS establish a connection? What did you interact with in the meantime?

HTTPS adds a layer of SSL/TLS protocol between HTTP and TCP, so the connection process in Jianli is as follows:

① First establish a TCP connection : three-way handshake.

② Then build SSL/TLS encryption :

Basic flow of SSL/TLS protocol:
  • The client requests and verifies the server's public key from the server.
  • Both parties negotiate to produce a "session key".
  • Both parties use the "session key" for encrypted communication.

The first two steps are the SSL/TLS establishment process, which is the handshake phase.

③ Send an HTTP request .

The summarized interview questions are also time-consuming. The articles will be updated from time to time, sometimes more than a few articles a day. If you help you review and consolidate your knowledge points, please support three consecutive times. The follow-up will be updated with billions of points!

Insert picture description here