Search code examples
socketsnetworkingtcpnetwork-programmingflow-control

How will a socket act when the data is sent faster than it can be processed?


I am not quite clear some detail mechanisms of TCP and sockets.

One client connects to a server through TCP, and sends data to server. What happens if the speed of sending is far greater than the speed of processing? For example, if the client sends 1MiB per second, but the server can only process 1 KiB per second, will it cause a system memory crash?

I know there's receive buffer size setting in the sockets API:

  1. What if I set the buffer size but the data is flooding?
  2. What if I don't set the receive buffer size?

Solution

  • What happens if the speed of sending is far greater than the speed of processing? For example, if the client sends 1 MiB per second, but the server can only process 1 KiB per second …

    If the sender is in blocking mode, it will block if it gets too far ahead of the receiver.

    If it's in non-blocking mode, send() will return -1 with errno == EAGAIN/EWOULDBLOCK.

    Will it cause a system memory crash?

    No.

    I know there's receive buffer size setting in the sockets API:

    1. What if I set the buffer size but the data is flooding?

    The receiving host will tell the sender to stop sending when the receive buffer is full.

    1. What if I don't set the receive buffer size?

    You'll get a default size.