Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

57. Sockets: UNIX Domain > Creating a Connected Socket Pair: socketpair()

Creating a Connected Socket Pair: socketpair()

Sometimes, it is useful for a single process to create a pair of sockets and connect them together. This could be done using two calls to socket(), a call to bind(), and then either calls to listen(), connect(), and accept() (for stream sockets), or a call to connect() (for datagram sockets). The socketpair() system call provides a shorthand for this operation.

#include <sys/socket.h>

int socketpair(int domain, int type, int protocol, int sockfd[2]);

Note

Returns 0 on success, or -1 on error

This socketpair() system call can be used only in the UNIX domain; that is, domain must be specified as AF_UNIX. (This restriction applies on most implementations, and is logical, since the socket pair is created on a single host system.) The socket type may be specified as either SOCK_DGRAM or SOCK_STREAM. The protocol argument must be specified as 0. The sockfd array returns the file descriptors referring to the two connected sockets.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint