socketpair(2)

๐Ÿ’– socketpair(2) ๐Ÿ’–

okay bestie, pull up a chair, we're doing the System Calls Manual thing and this one is genuinely so cute. it's socketpair(2) and it lives in the System Calls Manual, which is exactly where you'd expect a syscall to be hiding. no cap.


โœจ NAME โœจ

socketpair โ€” create a pair of connected sockets ๐Ÿซถ

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc). she's in libc, she's always been in libc.

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#include <sys/socket.h>

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

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so the socketpair() call creates an unnamed pair of connected sockets. like, two sockets. joined. bestie-coded. ๐Ÿ’…

and you get to pick:

  • the domain โ€” the specified domain she lives in
  • the type โ€” the specified type, obviously
  • the protocol โ€” optionally specified, so it's giving "only if you want to"

want the full lore on these three arguments? they're not doing a solo performance here, go read socket(2). โœจ

the file descriptors you use to reference your brand-new sockets get returned in sv[0] and sv[1]. and here's the iconic part: the two sockets are indistinguishable. twin flames. main character energy, doubled. you cannot tell them apart, that is the whole point, periodt. ๐Ÿ‘‘

๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

  • on success: zero is returned. slay.
  • on error: -1 is returned, errno is set to indicate the error, and sv is left unchanged.

and bestie, listen, this part matters: on Linux (and other systems), socketpair() does not modify sv on failure. a requirement standardizing this behavior was added in POSIX.1-2008 TC2. so your buffer stays pristine, no half-written chaos, we love a clean failure. ๐Ÿ˜ญ

๐Ÿ’€ ERRORS ๐Ÿ’€

  • EAFNOSUPPORT โ€” the specified address family is not supported on this machine. wrong vibe, wrong house, sorry babe.
  • EFAULT โ€” the address sv does not specify a valid part of the process address space. that pointer is NOT it.
  • EMFILE โ€” the per-process limit on the number of open file descriptors has been reached. you personally have too many tabs open, metaphorically.
  • ENFILE โ€” the system-wide limit on the total number of open files has been reached. it's not you, it's literally everyone.
  • EOPNOTSUPP โ€” the specified protocol does not support creation of socket pairs. this protocol is not a matchmaker, she does not do pairings.
  • EPROTONOSUPPORT โ€” the specified protocol is not supported on this machine. nope, not here, not today.

๐ŸŒธ VERSIONS ๐ŸŒธ

on Linux, the only supported domains for this call are AF_UNIX (or synonymously, AF_LOCAL) and AF_TIPC (since Linux 4.12). that's the whole list, bestie, don't go inventing more. ๐ŸŽ€

โญ STANDARDS โญ

POSIX.1-2024. certified, standard, official.

๐ŸŒˆ HISTORY ๐ŸŒˆ

POSIX.1-2001, 4.2BSD. she's been around, she's a legacy icon.

it is generally portable to/from non-BSD systems supporting clones of the BSD socket layer (including System V variants). so she travels well, iykyk. ๐Ÿงš

SOCK_CLOEXEC SOCK_NONBLOCK

  • POSIX.1-2024; Linux 2.6.27. (See socket(2).)

๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

pipe(2), read(2), socket(2), write(2), socket(7), unix(7)


okay that's the whole page, go make some socket pairs, and remember: do not try to tell sv[0] and sv[1] apart because you literally cannot, and that's the beautiful part. ๐Ÿ’–

this has been Linux man-pages 6.20, dated 2025-10-29, signing off like the absolute queen she is. bye bestie, drink water, close your file descriptors. ๐ŸŒธโœจ๐Ÿ’œ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms