15.3 socketpair Function
The socketpair function creates two sockets that are then connected together. This function applies only to Unix domain sockets.
#include <sys/socket.h> |
int socketpair(int family , int type , int protocol , int sockfd[2] ); |
Returns: nonzero if OK, -1 on error |
This function is similar to the Unix pipe function: Two descriptors are returned, and each descriptor is connected to the other. Indeed, Berkeley-derived implementations employ pipe by performing the same internal operations as socketpair (pp. 253254 of TCPv3).
The two created sockets are unnamed; that is, there is no implicit bind involved.The result of socketpair with a type of SOCK_STREAM is called a stream pipe . It is similar to a regular Unix pipe (created by the pipe function), but a stream pipe is full-duplex ; that is, both descriptors can be read and written. We show a picture of a stream pipe created by socketpair in Figure 15.7.
POSIX does not require full-duplex pipes. On SVR4, pipe returns two full-duplex descriptors, while Berkeley-derived kernels traditionally return two half-duplex descriptors (Figure 17.31 of TCPv3).