Receiving data in TCP

前端 未结 10 1249
借酒劲吻你
借酒劲吻你 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:36

    The transmission control protocol guarantees successful delivery of all packets by requiring acknowledgment of the successful delivery of each packet to the sender by the receiver. By this definition the receiver will always receive the payload in chunks when the size of the payload exceeds the MTU (maximum transmission unit).

    For more information please read Transmission Control Protocol.

    0 讨论(0)
  • 2020-12-18 05:37

    TCP guarantees that they will recieve all 1000 bytes, but not necessarily in order (though, it will appear so to the recieving application) and not necessarily all at once (unless you craft the packet yourself and make it so.).

    That said, for a packet as small as 1000 bytes, there is a good chance it'll send in one packet as long as you do it in one call to send, though for larger transmissions it may not.

    0 讨论(0)
  • 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
       }
    }
    
    0 讨论(0)
  • 2020-12-18 05:44

    The IP packets may get fragmented during retransmission.

    So the destination machine may receive multiple packets - which will be reassembled back by TCP/IP stack. Depending on the network API you are using - the data will be given to you either reassembled or in RAW packets.

    0 讨论(0)
  • 2020-12-18 05:45

    Basically as far as TCP goes it only guarantees that the data sent from one end to the other end will be sent in the same order. Now usually what you'll have to do is have an internal buffer that keeps looping until it has received your 1000 byte "packet". Because the recv command as mentioned returns how much has actually been received. So usually you'll have to then implement a protocol on top of TCP to make sure you send data at an appropriate speed. Because if you send() all the data in one run through it will overload the under lying networking stack, and which will cause complications. So usually in the protocol there is a tiny acknowledgement packet sent back to confirm that the packet of 1000 bytes are sent.

    0 讨论(0)
  • 2020-12-18 05:48

    See Transmission Control Protocol:

    TCP provides reliable, ordered delivery of a stream of bytes from a program on one computer to another program on another computer.

    A "stream" means that there is no message boundary from the receiver's point of view. You could get one 1000 byte message or one thousand 1 byte messages depending on what's underneath and how often you call read/select.

    Edit: Let me clarify from the application's point of view. No, TCP will not guarantee that the single read would give you all of the 1000 bytes (or 1MB or 1GB) packet the sender may have sent. Thus, a protocol above the TCP usually contains fixed length header with the total content length in it. For example you could always send 1 byte that indicates the total length of the content in bytes, which would support up to 255 bytes.

    0 讨论(0)
提交回复
热议问题