socket(2)
๐ socket(2) ๐
okay so bestie, this one's from the System Calls Manual and it's literally the moment everything begins. no cap. ๐ซถ
โจ NAME โจ
socket โ create an endpoint for communication ๐ธ
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ SYNOPSIS ๐
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
๐ DESCRIPTION ๐
socket() creates an endpoint for communication and hands you back a file descriptor that refers to that endpoint. and the file descriptor you get on a successful call is the lowest-numbered file descriptor not currently open for the process. like, it just slides into the first free slot. iconic behavior honestly. ๐
the domain argument ๐ฆ
domain says which communication domain you're in โ i.e. it picks the protocol family that'll be used for communication. these families are all defined in <sys/socket.h>, and the formats the Linux kernel currently understands are, in no particular order of slay:
AF_UNIXโ Local communication โ man pageunix(7)AF_LOCALโ Synonym forAF_UNIXAF_INETโ IPv4 Internet protocols โ man pageip(7)AF_AX25โ Amateur radio AX.25 protocol โ man pageax25(4)AF_IPXโ IPX - Novell protocolsAF_APPLETALKโ AppleTalk โ man pageddp(7)AF_X25โ ITU-T X.25 / ISO/IEC 8208 protocol โ man pagex25(7)AF_INET6โ IPv6 Internet protocols โ man pageipv6(7)AF_DECnetโ DECet protocol socketsAF_KEYโ Key management protocol, originally developed for usage with IPsecAF_NETLINKโ Kernel user interface device โ man pagenetlink(7)AF_PACKETโ Low-level packet interface โ man pagepacket(7)AF_RDSโ Reliable Datagram Sockets (RDS) protocol โ man pagesrds(7),rds-rdma(7)AF_PPPOXโ Generic PPP transport layer, for setting up L2 tunnels (L2TP and PPPoE)AF_LLCโ Logical link control (IEEE 802.2 LLC) protocolAF_IBโ InfiniBand native addressingAF_MPLSโ Multiprotocol Label SwitchingAF_CANโ Controller Area Network automotive bus protocolAF_TIPCโ TIPC, "cluster domain sockets" protocolAF_BLUETOOTHโ Bluetooth low-level socket protocolAF_ALGโ Interface to kernel crypto APIAF_VSOCKโ VSOCK (originally "VMWare VSockets") protocol for hypervisor-guest communication โ man pagevsock(7)AF_KCMโ KCM (kernel connection multiplexer) interfaceAF_XDPโ XDP (express data path) interface
more deets on all of the above address families, plus info on several other address families, live in address_families(7). go read it, it's giving encyclopedia but make it networking. ๐
the type argument ๐ง
your socket also has a type, and that's what specifies the communication semantics. currently defined types:
SOCK_STREAMโ Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be supported.SOCK_DGRAMโ Supports datagrams (connectionless, unreliable messages of a fixed maximum length).SOCK_SEQPACKETโ Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each input system call.SOCK_RAWโ Provides raw network protocol access.SOCK_RDMโ Provides a reliable datagram layer that does not guarantee ordering.SOCK_PACKETโ Obsolete and should not be used in new programs; seepacket(7).
not every protocol family implements every socket type. some of them just said "no <3" and honestly, respect.
the type flags (since Linux 2.6.27) โญ
since Linux 2.6.27, type does double duty! on top of naming a socket type, it can also carry the bitwise OR of any of these values, which tweak how socket() behaves:
SOCK_NONBLOCKโ Set theO_NONBLOCKfile status flag on the open file description (seeopen(2)) referred to by the new file descriptor. Using this flag saves extra calls tofcntl(2)to achieve the same result.SOCK_CLOEXECโ Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor. See the description of theO_CLOEXECflag inopen(2)for reasons why this may be useful.
the protocol argument ๐
protocol specifies a particular protocol to be used with the socket. normally only one protocol exists to support a particular socket type within a given protocol family โ in that case you can just pass protocol as 0 and vibe. but it's possible that many protocols exist, and in that case you gotta specify which particular one this way. the protocol number to use is specific to the "communication domain" in which communication is to take place; see protocols(5). and if you're wondering how to map protocol name strings to protocol numbers, that's a getprotoent(3) thing. ๐
stream sockets, the whole saga ๐
Sockets of type SOCK_STREAM are full-duplex byte streams. they do not preserve record boundaries. a stream socket has to be in a connected state before any data may be sent or received on it. you create a connection to another socket with a connect(2) call. once connected, data may be transferred using read(2) and write(2) calls, or some variant of the send(2) and recv(2) calls. when a session has been completed, a close(2) may be performed. out-of-band data may also be transmitted as described in send(2) and received as described in recv(2).
the communications protocols which implement a SOCK_STREAM ensure that data is not lost or duplicated. if a piece of data for which the peer protocol has buffer space cannot be successfully transmitted within a reasonable length of time, then the connection is considered to be dead. ๐ when SO_KEEPALIVE is enabled on the socket, the protocol checks โ in a protocol-specific manner โ if the other end is still alive. and a SIGPIPE signal is raised if a process sends or receives on a broken stream; this causes naive processes, which do not handle the signal, to exit. (not me getting one-shot by SIGPIPE ๐ญ)
SOCK_SEQPACKET sockets employ the same system calls as SOCK_STREAM sockets. the only difference is that read(2) calls will return only the amount of data requested, and any data remaining in the arriving packet will be discarded. also, all message boundaries in incoming datagrams are preserved. ๐
SOCK_DGRAM and SOCK_RAW sockets allow sending of datagrams to correspondents named in sendto(2) calls. datagrams are generally received with recvfrom(2), which returns the next datagram along with the address of its sender. ๐ฌ
SOCK_PACKET is an obsolete socket type to receive raw packets directly from the device driver. use packet(7) instead. (old and busted, fr fr.)
signals, ownership, and that whole drama ๐ฃ
an fcntl(2) F_SETOWN operation can be used to specify a process or process group to receive a SIGURG signal when the out-of-band data arrives, or SIGPIPE signal when a SOCK_STREAM connection breaks unexpectedly. this operation may also be used to set the process or process group that receives the I/O and asynchronous notification of I/O events via SIGIO. using F_SETOWN is equivalent to an ioctl(2) call with the FIOSETOWN or SIOCSPGRP argument.
when the network signals an error condition to the protocol module (e.g., using an ICMP message for IP) the pending error flag is set for the socket. the next operation on this socket will return the error code of the pending error. for some protocols, it's possible to enable a per-socket error queue to retrieve detailed information about the error; see IP_RECVERR in ip(7). ๐จ
socket level options โ๏ธ
the operation of sockets is controlled by socket level options. these options are defined in <sys/socket.h>. the functions setsockopt(2) and getsockopt(2) are used to set and get options. that's the whole system, bestie.
๐ RETURN VALUE ๐
on success, a file descriptor for the new socket is returned. on error, -1 is returned, and errno is set to indicate the error.
๐ ERRORS ๐
EACCESโ Permission to create a socket of the specified type and/or protocol is denied.EAFNOSUPPORTโ The implementation does not support the specified address family.EINVALโ Unknown protocol, or protocol family not available.EINVALโ Invalid flags intype.EMFILEโ The per-process limit on the number of open file descriptors has been reached.ENFILEโ The system-wide limit on the total number of open files has been reached.ENOBUFSorENOMEMโ Insufficient memory is available. The socket cannot be created until sufficient resources are freed.EPROTONOSUPPORTโ The protocol type or the specified protocol is not supported within this domain.
other errors may be generated by the underlying protocol modules. so like, not all of them are gonna be on this list. ๐
๐ฆ VERSIONS ๐ฆ
POSIX.1-2024 specifies SOCK_CLOFORK, but Linux doesn't support it. (we simply do not have it. moving on.)
๐ STANDARDS ๐
POSIX.1-2024.
๐ HISTORY ๐
POSIX.1-2001, 4.2BSD.
it's generally portable to/from non-BSD systems supporting clones of the BSD socket layer (including System V variants). โ๏ธ
the manifest constants used under 4.x BSD for protocol families are PF_UNIX, PF_INET, and so on, while AF_UNIX, AF_INET, and so on are used for address families. however, already the BSD man page promises: "The protocol family generally is the same as the address family", and subsequent standards use AF_* everywhere. so like, the PF_* versus AF_* thing is a whole historical situation. ๐
SOCK_NONBLOCKSOCK_CLOEXECโ POSIX.1-2024.
๐ธ EXAMPLES ๐ธ
an example of the use of socket() is shown in getaddrinfo(3). that's where the demo lives, go peek. ๐
๐ SEE ALSO ๐
accept(2), bind(2), close(2), connect(2), fcntl(2), getpeername(2), getsockname(2), getsockopt(2), ioctl(2), listen(2), read(2), recv(2), select(2), send(2), shutdown(2), socketpair(2), write(2), getprotoent(3), address_families(7), ip(7), socket(7), tcp(7), udp(7), unix(7)
- "An Introductory 4.3BSD Interprocess Communication Tutorial" and "BSD Interprocess Communication Tutorial", reprinted in UNIX Programmer's Supplementary Documents Volume 1.
and that's the whole page, bestie!! this all came to you from Linux man-pages 6.20, dated 2025-10-29 โ yes, that's literally the socket(2) moment. go make some endpoints, you're gonna slay. ๐ซถโจ๐
๐ฆ 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
- Copyright 1983-1991, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms