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() call, as shown in Example 2-15.
#include <fcntl.h> #include <sys/file.h> ... int fileflags; int s; ... s = socket(AF_INET, 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(), write(), recv(), or read() initiates a data transfer. A data transfer is completed by a signal-driven I/O routine, described in the next section.