Anastasios Andronidis
Anastasios Andronidis

Reputation: 6720

Why do I need socketpair() when I have socket() with AF_UNIX?

What confuses me is that given that sockets are bi-directional, why can't I just open 1 socket with socket() on the client and one on the server and let them communicate over this single socket?

What would be a common use case that I would need a pair of sockets?

Upvotes: 2

Views: 3068

Answers (2)

Useless
Useless

Reputation: 67713

So what is the common use case that I would need a pair of sockets?

Typically that you want bidirectional communication between a parent and child process (or sometimes between threads in the same process).

It's like a bidirectional equivalent of pipe, and avoids exposing an AF_UNIX path, or any other publicly-visible address, for something internal to your program.

There's a worked example here.

Upvotes: 6

Steffen Ullrich
Steffen Ullrich

Reputation: 123260

socketpair creates two sockets which are already connected to each other. A common use case is the communication between a parent and a child process: the parent creates the socket pair, forks the child and then child and parent can communicate through their end of the socket pair with each other.

Upvotes: 3

Related Questions