Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will send() ever block when using select()?

Tags:

c

select

sockets

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.

like image 411
lxgr Avatar asked Jan 15 '12 21:01

lxgr


1 Answers

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:

  1. Call select after every call to send to see if more data can be sent.

  2. 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.)

like image 83
Dietrich Epp Avatar answered Oct 21 '22 05:10

Dietrich Epp