When a socket is signalled as being OK to write by a call to select()
, how can I know how much data I can send without blocking? (In the case of full send buffers etc.)
Does inclusion in the set returned by select()
signify that the socket is ready for at least one byte of data, and will send()
then return a short count of written bytes?
Or will it block when I call send()
with a len
parameter that is bigger than the available buffer space? If so, how do I know the maximum amount?
I'm using regular C sockets on Linux.
The send
call should not block on the first call, and should send at least one byte on the first call -- assuming you are using a stream protocol and assuming it's not interrupted by a signal, etc. However, there are really only two ways to figure out how much data you can send:
Call select
after every call to send
to see if more data can be sent.
Put the socket in non-blocking mode, and call send
until it gives an EAGAIN
or EWOULDBLOCK
error.
The second option is preferred. (The third option is to do it in a different thread and simply let the thread block, which is also a good solution. In the past, threading implementations weren't as mature so non-blocking mode was seen as necessary for high-performance servers.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With