TCP provides flow control . The TCP stack (both on the sender side and on the receiver side) will be able to buffer some data for you, and this is usually done in the kernel of the OS.
When the receiverโs buffers are full, the sender will find out about it and stop sending more data, which will ultimately block the sending application (or, otherwise, will not be able to send more data) until the space becomes available again.
In a brief description, each TCP packet sent (segment) includes the size of the data that can be buffered โ the size of the window. This means that the other end always knows how much data it can send if the receiver does not throw it away because the buffers are full. If the window size becomes 0, the buffers fill up and data will no longer be sent (and if the sender is blocked, the send() call is blocked), theres a procedure to check if the tcp 0 window remains, so sending can resume again when the data has been used .
Here are some more details here.
source share