select
function and socket options
The book covers this material pretty exhaustively, so this page is sparser than most.
simpleselect.c
select.c
getsockopt.c
Here is the code for a non-blocking read on a file or socket
int flags; flags = fcntl(sock, F_GETFL,0); fcntl(sock,F_SETFL,flags | O_NONBLOCK);Now a call to
read
or recv
will return
immediately with a negative return value if
there is nothing to read, and errno
will
be set to EWOULDBLOCK