Skip to content

socketpair

socketpair() creates a pair of connected sockets.

It is useful for local parent/child communication and for building small IPC channels without binding a pathname socket.

Prototype

#include <sys/socket.h>

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

Typical Usage

int sv[2];

if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
    perror("socketpair");
    return -1;
}

Parameters

Parameter Description
domain Usually AF_UNIX
type Usually SOCK_STREAM or SOCK_DGRAM
protocol Usually 0
sv Output array containing two connected socket descriptors

Return Value

  • 0 on success
  • -1 on error, with errno set

Common Pitfalls

Warning

Close the unused socket end after fork(). Otherwise EOF detection may not work as expected.