Unpacking processing of TCP sticky packets

Source: Internet
Author: User

Because TCP is streaming, so the package has no boundaries, you must design a header, which indicates the length of the package (usually in bytes), according to this to split the package. If the frequency of sending/receiving is not high, generally do not do the unpacking process, because it is unlikely to have sticky bag phenomenon.

The following is an analysis of the sticky and split packages:

http://blog.csdn.net/zhangxinrun/article/details/6721495

Use QT's tcpsocket to read the data to split:

Http://www.aiuxian.com/article/p-1732805.html

I am based on the above link example QT logic to achieve, with boost ASIO to read, is synchronous:

1M_imp->m_thread = boost::make_shared<boost::thread>(2[=]()3             {4                  while(!boost::this_thread::interruption_requested ())5                 {6 boost::this_thread::interruption_point ();7 8                     Try9                     {Ten Boost::system::error_code EC; OneStd::vector<uint8_t> Tmpreadbuffer (1024x768* -); A                          -size_t bytes_transferred = m_imp->m_sockptr->Read_some (Boost::asio::buffer (Tmpreadbuffer), EC); -  theStd::cout <<"Byte transfered:"<< bytes_transferred <<"\ n"; -  -                         if(!EC) -                         { +                              -                             if(bytes_transferred = =0)Continue; +                              A                             if(Bytes_transferred <msg_head_size) at                             { -M_imp->m_readbuffer.insert (M_imp->m_readbuffer.end (), Tmpreadbuffer.begin (), Tmpreadbuffer.begin () + Bytes_ Transferred/sizeof(uint8_t)); -                                 Continue; -                             } -                             Else -                             { inM_imp->m_readbuffer.insert (M_imp->m_readbuffer.end (), Tmpreadbuffer.begin (), Tmpreadbuffer.begin () + Bytes_ Transferred/sizeof(uint8_t)); -                                  tosize_t totalsize = m_imp->m_readbuffer.size () *sizeof(uint8_t); +  -                                  while(totalsize) the                                 { *size_t msgsize = m_imp->Getmsglen (); $Std::cout <<"Msg Size is:"<< msgsize <<"\ n";Panax Notoginseng                                      -Std::vector<uint8_t>::const_iterator first = m_imp->M_readbuffer.begin (); theStd::vector<uint8_t>::const_iterator last = M_imp->m_readbuffer.begin () + msgsize/sizeof(uint8_t); +Std::vector<uint8_t>tmpmsg (First, last); A  theM_imp->M_msgqueue.push_back (tmpmsg); +  -M_imp->M_readbuffer.erase (First, last); $  $TotalSize = M_imp->m_readbuffer.size () *sizeof(uint8_t); -  -                                      the                                 } - Wuyi                             } the                              -                              Wu  -                             About  $  -                         } -                         Else -                         { AStd::cerr <<"recv Error:rac module! "<< ec.message () <<Std::endl; +M_imp->m_sockptr->close (); the                              Break; -                         } $                     } the                     Catch(std::exception&e) the                     { theStd::cerr << e.what () <<Std::endl; theM_imp->m_sockptr->close (); -                          Break; in                     } the                 } the             } About              the              the              the);
View Code

One of the following is a tcpsocket of QT, which has not been understood before, below is the underlying principle:

http://blog.csdn.net/ying_593254979/article/details/17006507

Unpacking processing of TCP sticky packets

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.