Network Programming: full package reading of streaming Socket Application Data

Source: Internet
Author: User

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 ){......}......}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.