Tcp message sticking and heartbeat packet sending problem

  TCP and UDP must be the most used protocols in communication. The specific protocol rules will not be introduced. Here, I just record some problems encountered in the project. The text is not organized. , no wonder....
  tcp problem 1: heartbeat packets
  need to keep the connection alive because of the long connection problem of tcp. The conventional method is to send heartbeat packets. deal with. Considering this requirement, mSocket.sendUrgentData(0xff); This method may be suitable, because the receiving end will automatically ignore this kind of message and does not need to deal with it by itself. In the test, it is found that the heartbeat is sent from the mobile phone to the pc through this method. The tcp connection of the pc is the xp system is normal and will not be disconnected. In the win7 system, the tcp connection will be disconnected on time in 80s, although there is a reconnection mechanism. , but frequent reconnection will still affect performance. After a period of investigation, I found that the reason is that on win7, the data sent by sendUrgentData can only be sent 16 times at most, and my heartbeat packet is sent every 5s, which is exactly 80s. Sending sendUrgentData data 16 times on win7 will cause network congestion and cause connection interruption.
  tcp problem 2: message sticking to packets
  When the mobile terminal receives the tcp message sent by the pc, it is found that there will be two or even more data packets, causing the two messages to stick together, and an error occurs when the receiver parses the message. Happened in bad time. In TCP, because of the three-way handshake principle, the data will receive ack feedback. If small data messages occur frequently, it will cause network congestion and affect performance. Therefore, in order to prevent frequent small data in the network, the bottom layer of tcp will be enabled. Nagle algorithm, each tcp socket has a buffer, each sending is actually taking data from this buffer and sending, only when the size of the data packet reaches the buffer size of the socket will it be sent, in the project Since I am sending a small string message, it will wait for the next message to reach a certain size and send it together. The size of the tcp socket buffer is determined by the size of the system kernel buffer, which also causes the message to be attached to the together. The solutions are as follows: 1. When sending multiple messages continuously, each message is sent at an interval of 40ms. There are two prerequisites for tcp to send messages. One is that the data size reaches the buffer size and is sent immediately, and the other is that the data does not reach the size of the buffer. The size of the buffer, but no data has entered the buffer in 40ms, and it will also be sent at this time (some say 200ms, but after testing, it is only 40ms in the linux system, and the Android kernel belongs to linux, so it only needs to be sent at an interval of 40ms); two , set tcp without delay, that is, do not enable the nagle algorithm, if not set, it will be enabled by default, the method of not enabling is also very simple, call mSocket.setTcpNoDelay(true); But in fact, this solution is not applicable when high-efficiency network communication is required, because you may not be able to endure the delay of 40ms every time you send a message, without enabling the nagle algorithm, sending small data frequently will cause Network congestion will also seriously affect the communication performance, so it is best to avoid such multiple transmissions in network programming. In the receiving mode, the "one send and one receive" mode should be maintained.
 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326564565&siteId=291194637