SOCKETPAIR(2)
π SOCKETPAIR β create a pair of connected sockets π
okay bestie, we're in section 2 of the manual, the System Calls Manual, and this one is literally the blueprint for two lil sockets that are sooo obsessed with each other they're basically twins. no cap.
β¨ LIBRARY β¨
Standard C Library (libc, -lc) β yeah girl, she's built in, you don't have to go hunting. π«Ά
π SYNOPSIS π
#include <sys/types.h>
#include <sys/socket.h>
int
socketpair(int domain, int type, int protocol, int *sv);
πΈ DESCRIPTION πΈ
so the socketpair() system call creates an unnamed pair of connected sockets, right? and you get to pick the communications domain, the type, and β if you're feeling extra β the protocol too.
the descriptors you use to reference these brand new sockets get handed back to you in sv[0] and sv[1]. and here's the iconic part: the two sockets are indistinguishable. like, truly. no main character, no sidekick, just two icons serving the exact same face. π
also!! the SOCK_CLOEXEC and SOCK_NONBLOCK flags in the type argument apply to both descriptors. both of them. equal treatment. we love to see it. π
β RETURN VALUES β
- if everything slays? the
socketpair()function returns the value0. β¨ - if it does NOT slay? the value
-1comes back and the global variableerrnogets set to tell you exactly what went wrong. π
π ERRORS π
the call succeeds unless β and bestie, listen, it's giving "here's the list of reasons it said no":
[EMFILE]β Too many descriptors are in use by this process. (you hoarded them. you did this. π)[EAFNOSUPPORT]β The specified address family is not supported on this machine. (not on this machine's bingo card.)[EPROTONOSUPPORT]β The specified protocol is not supported on this machine. (same energy, different flag.)[EOPNOTSUPP]β The specified protocol does not support creation of socket pairs. (it's just not that kind of protocol, ok?)[EFAULT]β The addresssvdoes not specify a valid part of the process address space. (that pointer is lost, fr fr. π)
π¦ SEE ALSO π¦
pipe(2), read(2), socket(2), write(2)
go read those too, they're the friend group. iykyk. π
π STANDARDS π
the socketpair() system call conforms to IEEE Std 1003.1-2001 (βPOSIX.1β) and IEEE Std 1003.1-2008 (βPOSIX.1β). certified. standards-compliant. periodt. β¨
π§ HISTORY π§
the socketpair() system call appeared in 4.2BSD. she's been around since forever and she's still thriving. legacy icon behavior. π
π BUGS π
This call is currently implemented only for the Unix domain.
translation: it's Unix-domain-only right now. don't come at her asking for anything else. π
that's the whole page bestie, go make some sockets that are obsessed with each other π
this was brought to you by Debian, dated February 10, 2018, from the SOCKETPAIR(2) page β mwah, take care of her π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms