What's the difference between pipes and sockets?

network-programming, networking, tcp

Solution

what are the differences between pipes and sockets, and when/how should you choose one over the other?

Both pipes and sockets handle byte streams, but they do it in different ways...

- pipes only exist within a specific host, and they refer to buffering between virtual files, or connecting the output / input of processes within that host. There are no concepts of packets within pipes.

- sockets packetize communication using IPv4 or IPv6; that communication can extend beyond localhost. Note that different endpoints of a socket can share the same IP address; however, they must listen on different TCP / UDP ports to do so.

Usage:

- Use pipes:

- when you want to read / write data as a file within a specific server. If you're using C, you `read()` and `write()` to a pipe.

- when you want to connect the output of one process to the input of another process... see popen()

- Use sockets to send data between different IPv4 / IPv6 endpoints. Very often, this happens between different hosts, but sockets could be used within the same host

BTW, you can use netcat or socat to join a socket to a pipe.

Problem

I found a couple of answers, but they seem to be specifically relating to Windows machines. So my question is what are the differences between pipes and sockets, and when/how should you choose one over the other?

Original source