views:

54

answers:

3

How does buffering work with sockets on Linux? i.e. if the server does not read the socket and the client keeps sending data. So what will happen? How big is the socket's buffer? And will the client know so that it will stop sending?

+1  A: 

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

Marcelo Cantos
A: 

I'm assuming that you're using send() and recv() for client and server communication.

So, send() will return the number of bytes that have been sent out. This doesn't necessarily equal to to the number of bytes you wanted to send out, so it's up to you to realise this and send the rest.

Now, the recv() returns the number of bytes read to the buffer. So if recv returns a 0, then the server has probably closed the connection.

aduric
+2  A: 

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

TCP, on the other hand, implements flow control. The server's kernel will gradually reduce the window, so the client will be able to send less and less data. At some point the window will go down to zero. At this point the client fills up its send buffer and receives an error from the send(2).

Nikolai N Fetissov