Asynchronous communication between processes is required in applications that handle multiple requests simultaneously. Asynchronous sockets must be
SOCK_STREAM type. To make a socket asynchronous, you issue a fcntl(2) call, as shown in Example 2-14.
#include <fcntl.h> #include <sys/file.h> ... int fileflags; int s; ... s = socket(AF_INET6, SOCK_STREAM, 0); ... if (fileflags = fcntl(s, F_GETFL ) == -1) perror("fcntl F_GETFL"); exit(1); } if (fcntl(s, F_SETFL, fileflags | FNDELAY | FASYNC) == -1) perror("fcntl F_SETFL, FNDELAY | FASYNC"); exit(1); } ...
After sockets are initialized, connected, and made asynchronous, communication is similar to reading and writing a file asynchronously. A send(3SOCKET), write(2), recv(3SOCKET), or read(2) initiates a data transfer. A data transfer is completed by a signal-driven I/O routine, described in the next section.