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.
- Void CNetClient: HandleMsg (char * buf, int nSize)
- {
- M_nRecvByteCount + = nSize;
- If (m_RecvTimer.GetMilliseconds ()> = 1000)
- {
- // Save the traffic size of the last 10 network packets
- M_RecvByte.AddToTail (m_nRecvByteCount );
- If (m_RecvByte.Count ()> 10)
- M_RecvByte.Remove (0 );
- M_nRecvByteCount = 0;
- M_RecvTimer.Start ();
- }
- M_PackBuffer.Put (buf, nSize );
-
- Int packsize = 0;
-
- Int bufsize = m_PackBuffer.TellPut ()-m_PackBuffer.TellGet ();
- While (TRUE)
- {
- PACKET_COMMAND * pPack = (PACKET_COMMAND *) m_PackBuffer.PeekGet ();
- If (pPack)
- {
- // Check whether the header is complete
- If (bufsize <PACKET_HEAD_SIZE)
- {
- Break;
- }
- Packsize = pPack-> GetSize ();
- // Check whether the network package is complete.
- If (bufsize <packsize)
- {
- Break;
- }
- If (! Packsize | packsize> sizeof (PACKET_COMMAND ))
- {
- M_PackBuffer.SeekGet (SEEK_HEAD, 0 );
- M_PackBuffer.SeekPut (SEEK_HEAD, 0 );
- Break;
- }
- PACKET_COMMAND pack;
- M_PackBuffer.Get (& pack, packsize );
- HandlePacket (& pack, packsize );
- Bufsize = m_PackBuffer.TellPut ()-m_PackBuffer.TellGet ();
- If (! Bufsize)
- {
- M_PackBuffer.SeekGet (SEEK_HEAD, 0 );
- M_PackBuffer.SeekPut (SEEK_HEAD, 0 );
- Break;
- }
- }
- }
- If (m_PackBuffer.TellGet () = m_PackBuffer.TellPut ())
- {
- M_PackBuffer.SeekGet (SEEK_HEAD, 0 );
- M_PackBuffer.SeekPut (SEEK_HEAD, 0 );
- }
- }
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!