0

A slight variation on this SO question.

Say the receiver expects packets to be at most 100 bytes. Say at time X there are actually 100 bytes available in the buffer, but for reasons the receiver only determines it needs to read 75 of those.

What happens with data not read from a socket?

Example:
Using flag MSG_PEEK (see here) the receiver determines that there is a full valid reply of 75 bytes in the buffer. The remaining 25 bytes must be the start of a next packet. The receiver elects to remove only 75 bytes (i.e. ::recv() without the MSG_PEEK flag) from the buffer, leaving 25 bytes unread/unmoved in the buffer.

10
  • 2
    Say the receiver expects packets to be at most 100 bytes. Wrong assumption. There are no packets in TCP; it is a stream of bytes. (and: fragmentation is unrelated) And : these 25 bytes have not been recieved yet, so the cannot be in the buffer. Commented Jul 21, 2021 at 13:01
  • 4
    Unread data remains to be read. Simple as that. Commented Jul 21, 2021 at 13:09
  • Your use of the word "packet" here is completely different -- has almost nothing to do with -- the Internet packets on which TCP is based. You're imagining that your sender wrote a 75-byte packet (really a message" or a "record") that your program is trying to read. But that sender's 75-byte message might have been sent as a 40-byte IP packet followed by a 35-byte IP packet, or it might have been concatenated with preceding or following data, and neither of you would ever know. Commented Jul 21, 2021 at 13:18
  • 1
    @iwarv I think you are confusing yourself by thinking too hard about this "buffer". Any buffer you might be talking about is an implementation detail, invisible/transparent to you. TCP simply guarantees that if the sender writes bytes B0 to Bn, you will receive exactly those same bytes, in order. Any question you might have about the behavior of your program, you can answer based on this guarantee, without worrying about any "buffer". Commented Jul 21, 2021 at 13:36
  • 1
    @iwarv You're not re-inventing the wheel. That's actually exactly what you should do. It minimizes the number of system calls required and keeps the socket buffers as empty as possible to improve throughput between the server and the client. Dealing with partial messages is quite trivial and you pretty much have already done it if you know how many bytes you need. Commented Jul 21, 2021 at 19:20

1 Answer 1

2

there are actually 100 bytes available in the buffer, but for reasons the receiver only determines it needs to read 75 of those.

I guess receiver refers to the application reading from the TCP socket. The remaining 25 bytes simply stay in the socket buffer to be read at some later time. If the socket is closed before that the data is lost.

Using the MSG_PEEK flag, the read data isn't removed from the buffer at all, so it still contains all 100 bytes after reading.

From the application level, you receive a continous data stream from a TCP socket. If and how the data was segmented or even fragmented for transport doesn't matter and isn't visible to the application. You can read the data in chunks of any size, regardless of how the source application has written it.

Say the receiver expects packets to be at most 100 bytes.

If you are trying to refer to TCP's Maximum Segment Size (MSS), the minimum Maximum Transfer Unit (MTU) for IPv4 is 576 bytes, so the minimum MSS is 536 bytes.

Sign up to request clarification or add additional context in comments.

2 Comments

With "packets" I mean the logical datagrams that the sender would stream to the receiver. At some level, all those streamed bytes have to resemble something, otherwise it is just noise. Mostly the receiver will allocate a buffer large enough to accommodate the largest such datagram, in case it's not broken into fragments.
TCP PDUs are called segments and they're transported encapsulated in IP packets. TCP uses send and receive windows as buffers. But still, you don't need to know anything about this if you just use a socket. Whether your 100 bytes are transported in a single segment or in ten segments doesn't matter one bit. IP packets that are too large for a certain link layer are broken up into several fragments but that is something completely different still.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.