TCP provides flow control . The TCP stack (both on the sender and receiver side) will be able to buffer some data for you, and this is usually done in the OS kernel.
When the receiver buffers fill up, the sender will know about it, and stop sending more data, eventually leading to the sending application blocking(or otherwise not being able to send more data) until space becomes available again.
Shortly described, every TCP packet(segment) sent includes the size of data that can be buffered - the window size. This means the other end at all times know how much data it can send without the receiver throwing it away because the buffers are full. If the window size becomes 0, buffers are full and no more data will be sent (and in case of the sender being blocking, a send()
call will block), Theres procedures for probing whether the tcp window is still 0, so sending can resume again when the data has been consumed.
There's some more details here
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…