How buffering works in socket on linux

How does socket buffering work on Linux? that is, if the server does not read the socket, and the client continues to send data. So what will happen? How big is the socket buffer? And will the customer know to stop sending?

+3
source share
3 answers

For a UDP socket, the client will never know - the server side will just start dropping packets after the receive buffer is full.

TCP, on the other hand, implements flow control . The server core gradually reduces the window, so the client will be able to send less and less data. At some point, the window will drop to zero. At this point, the client fills in its send buffer and receives an error message from send(2) .

+4
source

TCP sockets use buffering in the protocol stack. The stack itself implements flow control, so if the server buffer is full, it will stop sending more data to the client. Your code will see this as a blocked call to send() . The buffer size can vary greatly from a few kilobytes to several MB.

+1
source

I assume that you are using send() and recv() to communicate with the client and server.

So send() will return the number of bytes that were sent. This is not necessarily equal to the number of bytes you sent that you wanted , so you need to realize this and send the rest.

Now recv() returns the number of bytes read into the buffer. Therefore, if recv returns 0, then the server probably closed the connection.

+1
source

Source: https://habr.com/ru/post/1335667/


All Articles