Receive data in TCP

If I send 1000 bytes to TCP, does it guarantee that the receiver will receive 1000 bytes of "togther"? or maybe he will only get 500 bytes at first, and later will he get other bytes?

EDIT : The question comes from the application. If 1000 bytes are collected in one buffer before they reach the application, then I don’t care if it was fragmented on the way.

+6
c ++ c # networking tcp
source share
10 answers

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.

“Stream” means that, from the perspective of the receiver, there is no message boundary. You can receive one 1000-byte message or one thousand messages in 1 byte, depending on what is below and how often you call read / select.

Change Let me clarify in terms of application. No, TCP does not guarantee that a single read will give you all 1000 bytes (or 1 MB or 1 GB) that the sender could send. Thus, the protocol above TCP usually contains a fixed-length header with a total length of content in it. For example, you can always send 1 byte, which indicates the total length of the content in bytes, which supports up to 255 bytes.

+13
source share

As the other answers indicate, TCP is the stream protocol — each byte sent will be received (once and in the same order), but there are no internal “message boundaries” - whether all bytes are sent in one .send or several, they can still be received in one or more .receive calls.

So, if you need “message boundaries”, you need to overlay them on top of the TCP, IOW stream, essentially at the application level. For example, if you know that the bytes you send will never contain \0 , null-terminated lines work fine; various “escaping” methods allow sending byte strings that do not obey such restrictions. (There are existing protocols for this, but none of them are widespread or widespread).

+3
source share

Basically, since TCP is on, it only guarantees that data sent from one end to the other will be sent in the same order. Now usually you need to have an internal buffer that continues the loop until it receives a 1000-byte "packet". Because the recv command, as mentioned, returns how much was actually received. Therefore, you will usually need to run the protocol over TCP so that you can send data at the appropriate rate. Because if you send () all the data in one pass through it, it will overload the underlying network stack and which will cause complications. Thus, usually a tiny confirmation packet is sent in the protocol to confirm that a packet of 1000 bytes has been sent.

+2
source share

In your message, you determine how many bytes your message should contain. For example, in your case it is 1000. Below is the C # code and running it to achieve the same. The method returns 1000 bytes. Interrupt code - 0 bytes; You can tailor it according to your needs.

Application:

 strMsg = ReadData(thisTcpClient.Client, 1000, out bDisconnected); 

The following is the method:

  string ReadData(Socket sckClient, int nBytesToRead, out bool bShouldDisconnect) { bShouldDisconnect = false; byte[] byteBuffer = new byte[nBytesToRead]; Array.Clear(byteBuffer, 0, byteBuffer.Length); int nDataRead = 0; int nStartIndex = 0; while (nDataRead < nBytesToRead) { int nBytesRead = sckClient.Receive(byteBuffer, nStartIndex, nBytesToRead - nStartIndex, SocketFlags.None); if (0 == nBytesRead) { bShouldDisconnect = true; //0 bytes received; assuming disconnect signal break; } nDataRead += nBytesRead; nStartIndex += nBytesRead; } return Encoding.Default.GetString(byteBuffer, 0, nDataRead); } 

Let us know that this did not help you (0: Good luck.

+2
source share

Yes, it is possible to receive packages in parts. Hope this is an msdn article and the following example (taken from article in msdn for a quick look) would be useful for 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 } } 
+1
source share

TCP guarantees that they will receive all 1000 bytes, but not necessarily in order (although it will look the same as for the receiving application), and not necessarily all at once (unless you yourself create a package and do it).

However, for a packet up to 1000 bytes in size, there is a chance that it will send in one packet as much as you make it in one call before send , although it cannot be for large transfers.

0
source share

The only thing that guarantees the TCP level is that the recipient will receive:

  • all bytes transmitted by the sender
  • in the same order

There are no guarantees as to how bytes can be divided into “packets”. Anything you could read about MTU, packet fragmentation, maximum segment size, or anything else below the TCP socket level does not matter. TCP provides only the stream service.

As for your question, this means that the receiver can receive the first 500 bytes, and then the next 500 bytes. Or, the receiver can receive data one byte at a time, if that is what it asks for. For this reason, the recv() function accepts a parameter that tells it how much data needs to be returned, and not tell you how big the packet is.

0
source share

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

For more information, please read the Transmission Control Protocol .

-one
source share

IP packets may be fragmented during retransmission.

Thus, the destination machine can receive several packets that will be reassembled by the TCP / IP stack. Depending on the network API used, the data will be transferred to you either reassembled or in RAW packets.

-one
source share

It depends on the installed MTU (Maximum transfer unit). If your established connection (once a handshake) refers to a 512-byte MTU, you will need two or more TCP packets to send 1000 bytes.

-one
source share

All Articles