Next I will discuss the problem of package receiving in the previous article. The problem that I encountered today has been solved. TCP transmission, streaming socket programming, and reading network data, the entire application layer package is read from the buffered data, there was no bug in getting data packets with a fixed header length. Now, a packet with only a header is added, so the entire packet is messy. Integrity cannot be healed again. Now we need to reorganize the package extraction process. Msglen = Recv (S, buffer, sizeof (buffer), 0); buf4packet is the temporary data buffer for storing complete data packets
While (msglen> 0) {int Len, szcpy; Len = (offset <sizeof (comm_head )? Sizeof (comm_head): (comm_head *) buf4packet)-> msg_len); szcpy = len-offset; szcpy = (msglen> szcpy? Szcpy: msglen); memcpy (buf4packet + offset, buffer2, szcpy); offset + = szcpy; buffer2 + = szcpy; msglen-= szcpy; If (offset <sizeof (comm_head )) continue; Len = (comm_head *) buf4packet)-> msg_len; If (offset <Len) continue; If (offset! = Len) {xlog_out (log_com, p_fatal, "message_recv occur an error on Packet completeness analyze! \ N "); close (s); break;} offset = 0; comm_head * msg_p = NULL; msg_p = message_parse (buf4packet); If (msg_p! = NULL ){......}......}