Receiving data in TCP

前端 未结 10 1261
借酒劲吻你
借酒劲吻你 2020-12-18 04:59

If i send 1000 bytes in TCP, does it guarantee that the receiver will get the entire 1000 bytes \"togther\"? or perhaps he will first only get 500 bytes, and later he\'ll re

10条回答
  •  无人及你
    2020-12-18 05:43

    Yes, there is a chance for receiving packets part by part. Hope this msdn article and following example (taken from the article in msdn for quick review) would be helpful to you if you are using windows sockets.

    void CChatSocket::OnReceive(int nErrorCode)
    {
       CSocket::OnReceive(nErrorCode);
    
       DWORD dwReceived;
    
       if (IOCtl(FIONREAD, &dwReceived))
       {
          if (dwReceived >= dwExpected)   // Process only if you have enough data
             m_pDoc->ProcessPendingRead();
       }
       else
       {
          // Error handling here
       }
    }
    

提交回复
热议问题