C ++ Interview Questions summary - (CPP based on network programming)

1. main function is executed before the execution and implementation of what would later?

[A] before the main function is executed, mainly initialization function to initialize the system resources and related

  1. Set the stack pointer
  2. Static and global initialized static global variables, namely the content of the data segment
  3. The global variables are not initialized assignment, eg: numeric short int long as the 0, bool is false pointer is null, and the like, i.e. bss section
  4. Run global constructor, a function in C ++ constructor
  5. The main function of the parameter, argc, argv passed to the main function, etc., can really run

After the main function is executed, it runs the global object destructors
can _oneexit register a function, which is performed after main

If you need to add some code is executed after the main exit, you can use atexit () function, a registration function.


2. Network package sticky problem

Reference: https://blog.csdn.net/zhangxinrun/article/details/6721495

Long and short connection connector 2.1

【answer】

  1. Long connected
    client and server to establish the connection constantly open , and then the message sending and receiving messages
  2. Short connecting
    conduct when every client and server for packet communications connection is established, disconnect immediately after the transaction is completed for a multipoint communication, such as multiple client and a server

2.2 When do I need to consider sticky bag problem?

  1. If you use tcp each time you send data to establish a connection with each other, and then after the two sides finished piece of data to send, it closes the connection, so there is no problem sticking pack
    1.1 because there is only one package structure, similar to the http protocol, the main connection is closed both sides want to send a close connection.
  2. If no transmission data structures, such as file transfer, so that the sender just transmits, to the receiver just accepts storage ok, do not consider stick package
  3. If both Ding ~~ Establishment needs to be transmitted within a period of time after the connection of different data structures, it is generally necessary to add a length of the data packet in the header or the like, to ensure acceptance.

2.3 The reason appears stick package:

In the streaming, udp will not stick package, because it has a message boundaries (reference windows network programming)


  1. The sender need to wait for the buffer is full before sending out, will cause stick package
  2. The recipient does not receive timely packet buffer, resulting in multiple packages accepted

2.4 Solution

  1. For stick package phenomenon caused by the sender, the user can be programmed to avoid classes, TCP provides a push force instruction data transmitted immediately, that is not necessary to wait until the buffer is full sent,
  2. For the party to accept the package due to sticky, it can optimize program design, streamline the receiving process workload, increase the priority of the receiving process and other measures to make it in time to accept the data, thus avoiding problems stick package,
  3. Controlled by the recipient, the data in a packet structure field, in multiple human control receiver, and then combined to avoid stick package by this means

Three actions mentioned above, has its shortcomings.
While the method of programming a first set avoided due to the sender stick package, but it off optimization algorithm, reducing the network transmission efficiency, affect the performance of applications is not recommended.

The second method can reduce the possibility of stick package, but does not completely avoid stick package, when the high frequency transmission, since the network or a time period may cause a burst of data packets arrive faster receiver, receiver there still may be too late reception, resulting in stick package.

Although the third method avoids the stick package, but less efficient applications where real-time applications are not suitable.

2.5 Explaining Network

A. received first data1, data2 and received.
B. received first partial data data1, and data1 then receive all of the remaining portion, and data2.
C. received first part of the data of all the data of data1 and data2, and then the remaining received data data2.
D. disposable received the entire data of data1 and data2.

Why is there a case where BCD.
"Stick pack" may occur in the transmitting side may also occur at the receiving end.

1. caused by the Nagle algorithm sender stick package : Nagle algorithm is an algorithm to improve network transmission efficiency Simply put, when we submitted some data to TCP, TCP does not send data at once this section, but wait. a short time, while waiting to see whether there is data to be sent, if this time it will send out two pieces of data. this is a simple explanation Nagle algorithm, a detailed look at the books. like C and the case D Nagle algorithm is likely to be caused.

NOTE: Nagle algorithm for automatically connecting a number of smaller message buffer; process (referred Nagling) to increase the efficiency of the network by reducing the number of software packages to be sent.

2. The receiving end receives the receiving side is not timely stick package : TCP data received will have its own buffer, and notifies the application layer data taken when for some reason the application layer of the TCP can not be timely data fetch. out, it will cause TCP buffer data stored in a few paragraphs.

2.6 UDP packet will be sticky problem?

【answer】

  1. For UDP, the merger will not block the use of optimization algorithms , so that, in fact, now that, due to the many support UDP mode, the receiving end of the skbuff (socket buffer) employed to record each chain structure a UDP packet arriving at each have a UDP packet (information source address and port) message header, so that, for the receiving side, it is easy to distinguish the process. So UDP will not stick package problem

2.7 protection message boundaries and flow

Reference: https://blog.csdn.net/tiandijun/article/details/41961785


Borders and protect the message flow

So what is to protect the borders and news flow it?

Protection message boundaries , refers to the data transfer protocol as a separate message transmitted over the Internet, the receiver can only receive a separate message. That the presence of the protective boundary message, the receiving side can only receive a data packet sent from the console. Refers to the stream for the unprotected protected message boundaries, if the transmitting end continuously transmits data, the receiving end is possible in a receiving operation, it receives two or more packets.

For example, we send three consecutive data packet size are 2k, 4k, 8k, these three data packets, the receiving end has been reached have a network stack, if using the UDP protocol, no matter how we use the receive buffer to receive data, we must have received three times the action, to be able to complete all of the data packets received while using the TCP protocol, we just put in 14k or more, we can once all the packets received down the receiving buffer size settings, only there is a need to receive action.

note:

This is because the protection of the UDP protocol message boundaries so that each message is independent. And yet the streaming data as a string of data flow, he did not think the data is a message. There are so many people in the use of tcp protocol for communication, tcp is not clear based transport stream, when the continuous transmission of data, they will often know tcp packet loss. In actual fact, because when they use a buffer large enough, they might once have received two or more packets, and many people tend to ignore it, just check parsing the first packet, and other packets that have been received have been neglected. If you want to make time so we kind of network programming, we must pay attention to this.


3. The memory pool design

Reference STL source resolution, including the introduction of STL memory pool

4. Network Programming encryption algorithm

4.1 How to solve the security problems of data transmission?

4.2 encryption algorithm

Guess you like

Origin blog.csdn.net/shaoye_csdn1/article/details/90634806