Code Implementation case for solving the problem of sticking packets between iPhone clients and servers

Source: Internet
Author: User

IPhone ClientAndServerThe implementation case of inter-stick packet problem solving is described in this article. If yourIPhoneThe App mustServerWhen data is exchanged, the server may encounter a situation where two messages are sent consecutively: The first message is received accurately, but the second message is not completely received. You can try the Code provided in this post on the CocoaChina forum to solve the problem of sticking packets between the iPhone client and the server.

 
 
  1. Void CNetClient: HandleMsg (char * buf, int nSize)
  2. {
  3. M_nRecvByteCount + = nSize;
  4. If (m_RecvTimer.GetMilliseconds ()> = 1000)
  5. {
  6. // Save the traffic size of the last 10 network packets
  7. M_RecvByte.AddToTail (m_nRecvByteCount );
  8. If (m_RecvByte.Count ()> 10)
  9. M_RecvByte.Remove (0 );
  10. M_nRecvByteCount = 0;
  11. M_RecvTimer.Start ();
  12. }
  13. M_PackBuffer.Put (buf, nSize );
  14.  
  15. Int packsize = 0;
  16.  
  17. Int bufsize = m_PackBuffer.TellPut ()-m_PackBuffer.TellGet ();
  18. While (TRUE)
  19. {
  20. PACKET_COMMAND * pPack = (PACKET_COMMAND *) m_PackBuffer.PeekGet ();
  21. If (pPack)
  22. {
  23. // Check whether the header is complete
  24. If (bufsize <PACKET_HEAD_SIZE)
  25. {
  26. Break;
  27. }
  28. Packsize = pPack-> GetSize ();
  29. // Check whether the network package is complete.
  30. If (bufsize <packsize)
  31. {
  32. Break;
  33. }
  34. If (! Packsize | packsize> sizeof (PACKET_COMMAND ))
  35. {
  36. M_PackBuffer.SeekGet (SEEK_HEAD, 0 );
  37. M_PackBuffer.SeekPut (SEEK_HEAD, 0 );
  38. Break;
  39. }
  40. PACKET_COMMAND pack;
  41. M_PackBuffer.Get (& pack, packsize );
  42. HandlePacket (& pack, packsize );
  43. Bufsize = m_PackBuffer.TellPut ()-m_PackBuffer.TellGet ();
  44. If (! Bufsize)
  45. {
  46. M_PackBuffer.SeekGet (SEEK_HEAD, 0 );
  47. M_PackBuffer.SeekPut (SEEK_HEAD, 0 );
  48. Break;
  49. }
  50. }
  51. }
  52. If (m_PackBuffer.TellGet () = m_PackBuffer.TellPut ())
  53. {
  54. M_PackBuffer.SeekGet (SEEK_HEAD, 0 );
  55. M_PackBuffer.SeekPut (SEEK_HEAD, 0 );
  56. }
  57. }

Summary:IPhone ClientAndServerThe content of the code implementation case for solving inter-stick packet problems has been introduced. I hope this article will help you!

Related Article

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.