With non-blocking sockets, the accept(), connect() and all input/output functions are terminated if they cannot be executed immediately. The function concerned then returns an error code. In other words, in contrast to normal sockets, non-blocking sockets prevent a process from being interrupted because it has to wait for the termination of accept(), connect() or I/O functions. You can mark a socket created with s=socket() as non-blocking with the soc_ioctl() function (see section "soc_ioctl() (ioctl) - control sockets") as follows:
#include <ioctl.h> ... int s; ... int block; s = socket(AF_INET, SOCK_STREAM, 0); ... block = 1; ... if (soc_ioctl(s, FIONBIO, &block) < 0) { perror("soc_ioctl(s, FIONBIO, block) <0"); exit(1); } ...
You should particularly watch out for the EWOULDBLOCK error when executing the accept(), connect() or I/O functions on non-blocking sockets. EWOULDBLOCK is stored in the global errno variable and occurs if a function which normally blocks is executed on a non-blocking socket.
The accept() and connect() functions as well as all read and write operations can return the EWOULDBLOCK error code. Processes should therefore be prepared to handle such return values: for example, even if the send() function is not executed completely, it may still be meaningful with stream sockets to execute at least part of the write operations. In this case, send() only considers the data that can be sent immediately. The return value indicates the amount of data already sent.
The “non-blocking” property of a listen socket is not passed onto a socket created with accept().