Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

A Unix domain socket is a local communication endpoint that lets processes on the same machine exchange data through the socket API. On Linux, its address family is called AF_UNIX or AF_LOCAL; unlike AF_INET and AF_INET6, it is not an Internet address family.

How a Unix domain socket works

Two processes use sockets to communicate without sending traffic to another machine. The socket API supplies the endpoint and communication operations; the socket type determines whether the application exchanges a continuous stream or separate messages. The Linux unix(7) manual describes the family as being used for communication between processes on the same machine.

A stream socket does not preserve application message boundaries. If an application sends several messages over a stream, it must define a framing method—such as a length prefix or delimiter—so the receiving process can tell where one message ends and the next begins.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a socket type for the communication pattern

Type Communication pattern What the application should account for
SOCK_STREAM Connection-oriented byte stream Define framing if the application needs distinct messages.
SOCK_DGRAM Datagrams that retain message boundaries Linux documents Unix-domain datagrams as reliable and unordered in the terms of its implementation; do not assume those semantics are a cross-platform guarantee.
SOCK_SEQPACKET Connection-oriented messages, preserving boundaries and order Check support and behavior on the target operating system. Linux has supported this type since Linux 2.6.4, according to unix(7).

These types describe different communication contracts, not a universal performance ranking. Select one based on whether the application needs a byte stream, independent messages, or ordered messages on a connection, and verify the target operating system’s documentation.

How Unix domain socket addresses work

A Unix domain socket address can be unnamed or named. Named addresses may use a filesystem pathname; Linux also supports an abstract namespace. These forms differ in portability and access-control behavior.

Filesystem pathname

A pathname socket appears as a socket entry in the filesystem. On Linux, its ownership and permissions can be managed with filesystem operations, which can help control access. The application or administrator must remove the socket entry when it is no longer needed, as described by unix(7).

Rank #2
Sale
The Unix Programming Environment (Prentice-Hall Software Series)
  • The Unix Programming Environment (Prentice-Hall Software Series)
  • Product Type: ABIS_BOOK
  • Pearson

Linux abstract namespace

On Linux, an abstract address begins with a null byte in sun_path and is independent of filesystem pathnames. Because it has no filesystem entry, filesystem permissions do not control access to it. The abstract namespace is a Linux-specific, nonportable extension; use a pathname form when portability is required and supported. Address layout and length rules can also differ by operating system, so check the platform’s sockaddr_un documentation. See unix(7) and socket(2).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What Unix domain sockets can carry

In addition to ordinary data, Unix domain sockets can support ancillary data, including file descriptors or process credentials. Passing an open file descriptor can let one process provide another with access to an already-open resource. Credential handling and descriptor-passing rules depend on the operating system, socket type, and relevant APIs; these features are not, by themselves, a universal guarantee that a peer is authenticated. The Linux details are documented in unix(7).

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When to use one—and what to compare

Unix domain sockets are a fit when processes on one machine need socket-based communication. To decide whether they suit a design, compare the actual requirements rather than assuming that one IPC mechanism is always faster or safer.

  • Locality: Unix domain sockets are for processes on the same machine. If peers must communicate across machines, use a network-capable mechanism instead.
  • Message handling: Choose between a stream that needs application framing, datagrams that preserve message boundaries, and sequenced packets that preserve boundaries and ordering where supported.
  • Address portability: Pathname addresses are the more portable choice where the target systems support them. Linux abstract addresses are not portable.
  • Access control: A pathname socket can use filesystem ownership and permission controls on Linux. Abstract addresses have no filesystem permissions, so plan for a different access-control approach.
  • Resource transfer: If the design benefits from passing file descriptors or credentials, verify the specific platform’s ancillary-data APIs and rules.

For comparison with pipes, TCP loopback, or other IPC methods, ask whether communication must cross machines, whether message boundaries and ordering matter, how peers will be authorized, and whether descriptor passing is useful. The Linux references establish the socket-family and type behavior described above; the best choice depends on the application and target platform.

Quick Recap

SaleBestseller No. 2
The Unix Programming Environment (Prentice-Hall Software Series)
The Unix Programming Environment (Prentice-Hall Software Series)
The Unix Programming Environment (Prentice-Hall Software Series); Product Type: ABIS_BOOK; Pearson
$77.35
SaleBestseller No. 3
SaleBestseller No. 4

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.