The most common questions in C++ interviews (4) - network programming part

Series Article Directory

#C++ interview most common questions (1)

#C++ interview most frequently asked questions (2)

#C++ interview most frequently asked questions (3)

1. What is the OSI seven-layer model? What is the main task of each layer?

The OSI seven-layer model is the application layer, presentation layer, session layer, transport layer, network layer, data link layer and physical layer from top to bottom.
The role of each layer is:
Application layer: Through the interaction between application processes, a specific network application is formed.
Presentation layer: Solve the representation problem of information exchanged by the communication parties.
Session layer: solve the problem of inter-process session.
Transport layer: solves the problem of network-based communication between processes.
Network Layer: The problem of packets being transmitted over multiple networks.
Data link layer: The transmission problem of packets on a link.
Physical layer: What kind of signal is used for transmission.

2. Briefly describe the three-way handshake and four-way wave

(1) TCP three-way handshake

The first handshake: When the connection is established, the client sends a syn packet (syn=x) to the server, and enters the SYN_SENT state, waiting for the server to confirm; SYN: Synchronize Sequence Numbers.

The second handshake: the server receives the syn packet, must confirm the client's SYN (ack=x+1), and at the same time send a SYN packet (syn=y), that is, the SYN+ACK packet, and the server enters the SYN_RECV state at this time;

The third handshake: the client receives the SYN+ACK packet from the server and sends an acknowledgment packet ACK (ack=y+1) to the server. After the packet is sent, the client and server enter the ESTABLISHED (TCP connection is successful) state and complete three times shake hands.

(2) TCP waves four times

1) The client process sends a connection release message and stops sending data. Release the header of the data message, FIN=1, and its sequence number is seq=u (equal to the sequence number of the last byte of the previously transmitted data plus 1), at this time, the client enters FIN-WAIT-1 (stop waiting 1) Status.
2) The server receives the connection release message, sends a confirmation message, ACK=1, ack=u+1, and brings its own serial number seq=v, at this time, the server enters CLOSE-WAIT (close waiting )state.
3) After the client receives the confirmation request from the server, at this time, the client enters the FIN-WAIT-2 (terminate waiting 2) state, waiting for the server to send a connection release message (before this, it needs to accept the last message sent by the server) data).
4) After the server sends the last data, it sends a connection release message to the client, FIN=1, ack=u+1, because it is in the half-closed state, the server probably sent some more data, assuming that at this time The serial number is seq=w. At this time, the server enters the LAST-ACK (final confirmation) state, waiting for the client's confirmation.
5) After the client receives the connection release message from the server, it must send an acknowledgment, ACK=1, ack=w+1, and its own serial number is seq=u+1. At this time, the client enters TIME- WAIT (wait for time) state.
6) As long as the server receives the confirmation from the client, it will immediately enter the CLOSED state. Similarly, after the TCB is revoked, this TCP connection ends.

(3) The reason for the three-way handshake when connecting, but the four-way handshake when closing

Because when the server side receives the SYN connection request message from the client side, it can directly send the SYN+ACK message. Among them, the ACK message is used for response, and the SYN message is used for synchronization. But when the connection is closed, when the server receives the FIN message, it may not close the SOCKET immediately, so it can only reply an ACK message first, telling the client, "I have received the FIN message you sent". I can only send the FIN message until all the messages on my Server end have been sent, so they cannot be sent together. Therefore, a four-step handshake is required.

3. What are TCP and UDP, and what are the connections and differences between them?

TCP:
Transmission Control Protocol (TCP, Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol.
UDP:
The Internet protocol set supports a connectionless transport protocol called User Datagram Protocol (UDP, User Datagram Protocol). UDP provides a way for applications to send encapsulated IP packets without establishing a connection.
Summary of the difference between TCP and UDP:

(1) TCP is connection-oriented, establishes a connection through a three-way handshake, and waves four times to connect and remove the connection; UDP is connectionless, that is, it does not need to establish a connection before sending data. This method brings efficient transmission efficiency to UDP, but also As a result, it is impossible to ensure that the data is sent successfully.
(2) TCP is a reliable communication method. For the data transmitted through the TCP connection, TCP uses methods such as timeout retransmission and data verification to ensure that the data is error-free, not lost, not repeated, and arrives in order; and UDP will be transmitted at the maximum speed because it does not require a connection , but reliable delivery is not guaranteed, that is, problems such as loss and duplication will occur.
(3) TCP is oriented to byte streams. In fact, TCP regards data as a series of unstructured byte streams. Due to connection problems, when the network fluctuates, the connection may have response problems; UDP is message-oriented, and UDP There is no congestion control, so network congestion does not slow down the sending rate of the source host.
(4) Each TCP connection can only be point-to-point; UDP does not establish a connection, so it can support one-to-one, one-to-many, many-to-one and many-to-many interactive communications, that is, it can accept multiple people at the same time package.
(5) TCP needs to establish a connection, and the header overhead of 20 bytes is relatively large compared to 8 bytes of UDP.
(6) The logical communication channel of TCP is a full-duplex reliable channel, while UDP is an unreliable channel.

4. TCP and UDP usage scenarios

UDP usage scenario:
Therefore, UDP does not provide a complicated control mechanism, and uses IP to provide connectionless communication services. Data can be sent at any time, and the processing is simple and efficient. Therefore, it is mainly used in the following scenarios:
(1) Communication with a small total amount of packets (DNS, SNMP)
(2) Video, audio and other multimedia communications (instant messaging)
(3) QQ is the UDP protocol used.
(4) Broadcast communication

Note: Mainly in all scenarios where speed is pursued

TCP usage scenario:
TCP usage scenario: Compared with UDP, TCP implements various controls in the data transmission process, can perform retransmission control when packets are lost, and can also perform sequence control on out-of-order packets. In the case of high reliability requirements, TCP can be used, that is, when UDP is not considered, TCP can be selected.

Note: In particular, reliable connections are required, such as payment, encrypted data, etc., all need to rely on TCP.

5. How does TCP maintain a reliable communication method?

(1) Data Fragmentation: Fragmentation of user data at the sending end, reorganization at the receiving end, TCP determines the size of the fragmentation and controls fragmentation and reassembly; (2) Arrival confirmation: The receiving end receives the fragmented
data , send an acknowledgment packet to the sender according to the sequence number of the fragmented data;
(3) Timeout retransmission: the sender counts the time after sending the fragment, if the timeout does not receive the corresponding acknowledgment packet, the corresponding fragment will be resent ;
(4) Sliding window: The receiving buffer space of both sides of the TCP connection is fixed, and the receiving end can only accept the data that the buffer can accommodate.
(5) Out-of-order processing: The receiving end of TCP needs to reorder the received data.
(6) Duplication processing: If the transmitted TCP fragments are duplicated, the receiving end of TCP needs to discard the duplicated data.
(7) Data verification: TCP detects any changes in data during transmission by maintaining the checksum of its header and data.

6. Can the three-way handshake in TCP be changed to two handshakes?

Can't.
First of all, TCP is full-duplex communication, and TCP achieves reliable transmission through serial numbers, so a connection must first synchronize the initial serial number (ISN) of both parties to ensure the data accuracy of both parties; and the generation of ISN The rules tell us that only the party that generates the ISN can confirm whether the ISN is correct. Therefore, three handshakes are required to ensure the synchronization of the ISNs of the two communicating parties. If there are only two handshakes, only the ISN of the initiator can be confirmed. It can guarantee the data reliability of the one-way communication from the initiator to the server.
In addition, TCP also uses ISN to solve the data confusion problem caused by historical connection initialization during the three-way handshake process.

7. How does UDP achieve reliable transmission?

UDP is not a connection-type protocol, so it has the advantages of low resource consumption and fast processing speed, so usually audio, video and ordinary data use UDP more when transmitting, because they will not lose one or two data packets occasionally. have too much influence on the receiving result.
The transport layer cannot guarantee the reliable transmission of data, it can only be realized through the application layer. The implementation method can refer to the tcp reliable transmission method, but the implementation is not at the transport layer, and the implementation is transferred to the application layer.
Implement confirmation mechanism, retransmission mechanism, and window confirmation mechanism.
If you do not use the linux protocol stack and the upper-layer socket mechanism to achieve reliable transmission by capturing and sending packets, the following functions must be implemented: sending: packet fragmentation, packet confirmation, and packet retransmission Reception
:
packet Sequencing and sequence number confirmation of packets
At present, there are the following open source programs that use udp to achieve reliable data transmission. They are RUDP, RTP, and UDT respectively.

8. What is the ten tcp sticky packet problem? How to solve?

TCP sticky packets means that several packets of data sent by the sender are glued into one packet when they arrive at the receiver. From the perspective of the receiving buffer, the head of the next packet of data follows the tail of the previous packet of data. The reason for the sticky packets is In many ways, it may be from the sender or the receiver.
1) sender

For the sticky packet problem caused by the sender, it can be solved by turning off the Nagle algorithm, and using the TCP_NODELAY option to turn off the algorithm.

(2) Receiver

The receiver has no way to deal with the sticky packet phenomenon, and can only hand over the problem to the application layer for processing.

(2) Application layer

The solution at the application layer is simple and feasible. It can not only solve the sticky packet problem of the receiver, but also solve the sticky packet problem of the sender.

Solution: loop processing, when the application reads packets from the receiving buffer, after reading one piece of data, it should read the next piece of data in a loop until all the data is processed, but how to judge the length of each piece of data?

Format data: Each piece of data has a fixed format (start character, end character). This method is simple and easy, but when selecting the start character and end character, be sure to ensure that each piece of data does not contain the start character and end character.
Send length: When sending each piece of data, send the length of the data together. For example, the first 4 bits of the data are specified as the length of the data. The application layer can judge the start and end positions of each packet according to the length during processing.

9. Briefly describe the basic steps of socket communication

Specifically divided into two parts:

(1) Server
1) socket (create socket)
2) bind (bind socket and port number)
3) listen (listen to the port number)
4) accept (wait and accept client connection request)
5) read, write ( Read data and return data)
6) close (close socket)

(2) Client
1) socket (create socket)
2) connect (connect to specified port)
3) read, write (read data and return data)
4) close (close socket)

10. What are http and https? What's the difference?

HTTP: Hypertext Transfer Protocol (HTTP, HyperText Transfer Protocol) is the most widely used network protocol on the Internet. HTTP was originally designed to provide a way to publish and receive HTML pages. It can make browsers more efficient. The HTTP protocol sends information in clear text. If a hacker intercepts the transmission message between the Web browser and the server, he can directly obtain the information.

Principles of HTTP:
1) The client's browser must first establish a connection with the server through the network. The connection is completed through TCP, and the port number of the general TCP connection is 80. After the connection is established, the client sends a request to the server. The format of the request is: Uniform Resource Identifier (URI), protocol version number, followed by MIME information including request modifiers, client information and licensed content.
2) After receiving the request, the server will give corresponding response information, the format of which is a status line, including the protocol version number of the information, a success or error code, followed by MIME information including server information, entity information and possible content.
HTTPS: It is an HTTP channel aimed at security, and it is a secure version of HTTP. The security foundation of HTTPS is SSL. The SSL protocol is located between the TCP/IP protocol and various application layer protocols, providing security support for data communication. The SSL protocol can be divided into two layers: SSL Record Protocol (SSL Record Protocol), which is based on a reliable transmission protocol (such as TCP) and provides support for basic functions such as data encapsulation, compression, and encryption for high-level protocols. SSL Handshake Protocol (SSL Handshake Protocol), which is based on the SSL record protocol, is used for identity authentication, negotiation of encryption algorithms, and exchange of encryption keys before the actual data transmission begins.

The difference between the two:

1. The HTTPS protocol needs to apply for a certificate from a CA (Certificate Authority, certificate authority). Generally, there are few free certificates, so a certain fee is required. (The former NetEase official website is http, and NetEase mailbox is https.

2. HTTP is a hypertext transfer protocol, and information is transmitted in plain text, while HTTPS is a secure SSL encrypted transfer protocol.

3. HTTP and HTTPS use completely different connection methods and different ports. The former is 80 and the latter is 443.

4. The HTTP connection is very simple and stateless. The HTTPS protocol is a network protocol constructed by the SSL+HTTP protocol that can perform encrypted transmission and identity authentication, and is safer than the HTTP protocol. (Stateless means that the sending, transmission, and reception of its data packets are independent of each other. Connectionless means that both parties to the communication do not maintain any information about each other for a long time.)


Guess you like

Origin blog.csdn.net/qq_46901210/article/details/124792797