listen(2)
โจ listen(2) โ the socket glow-up era โจ
okay bestie so this is a page from the System Calls Manual, it's called listen and it lives in section 2, and honestly? it's giving main character energy for every server you've ever run. ๐
๐ธ LIBRARY ๐ธ
Standard C library (libc, -lc). yes queen, it's just the plain libc, no extra drama.
๐ฆ SYNOPSIS ๐ฆ
#include <sys/socket.h>
int listen(int sockfd, int backlog);
๐ DESCRIPTION ๐
So listen() takes the socket that sockfd points at and marks it as a passive socket, meaning it's now a socket that's gonna sit there and accept incoming connection requests using accept(2). it's literally the "i'm open for business" toggle. โจ
The sockfd argument is a file descriptor that refers to a socket of type SOCK_STREAM or SOCK_SEQPACKET. no other types, those two only, slay.
The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow. like, how long can the line outside the club get before the bouncer is like "sorry babe, capacity." If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later reattempt at connection succeeds. so it's either a hard no or a "try again later bestie", depending on the protocol. ๐
๐ RETURN VALUE ๐
On success, zero is returned. On error, -1 is returned, and errno is set to indicate the error. that's it, that's the whole vibe check.
๐ ERRORS ๐
EADDRINUSEโ Another socket is already listening on the same port. she's taken, sorry.EADDRINUSEโ (Internet domain sockets) The socket referred to bysockfdhad not previously been bound to an address and, upon attempting to bind it to an ephemeral port, it was determined that all port numbers in the ephemeral port range are currently in use. See the discussion of/proc/sys/net/ipv4/ip_local_port_rangeinip(7). yeah this one hits twice, same name, different lore โ that's on purpose, don't come for me.EBADFโ The argumentsockfdis not a valid file descriptor. not a real fd, blocked. ๐ENOTSOCKโ The file descriptorsockfddoes not refer to a socket. it's an fd but it's not that kind of fd. imposter behavior.EOPNOTSUPPโ The socket is not of a type that supports thelisten()operation. wrong socket type, no listen for you.
๐ STANDARDS ๐
POSIX.1-2024. periodt.
๐ง HISTORY ๐ง
4.2BSD, POSIX.1-2001. been iconic for literal decades.
๐ฅ NOTES ๐ฅ
To accept connections, the following steps are performed โ this is the sacred sequence, memorize it:
- A socket is created with
socket(2). - The socket is bound to a local address using
bind(2), so that other sockets may beconnect(2)ed to it. - A willingness to accept incoming connections and a queue limit for incoming connections are specified with
listen(). - Connections are accepted with
accept(2).
The behavior of the backlog argument on TCP sockets changed with Linux 2.2. Now it specifies the queue length for completely established sockets waiting to be accepted, instead of the number of incomplete connection requests. The maximum length of the queue for incomplete sockets can be set using /proc/sys/net/ipv4/tcp_max_syn_backlog. When syncookies are enabled there is no logical maximum length and this setting is ignored. See tcp(7) for more information. so if you're out here trusting your backlog number with syncookies onโฆ it's giving ignored, bestie. ๐ญ
If the backlog argument is greater than the value in /proc/sys/net/core/somaxconn, then it is silently capped to that value. silently. no warning, no note, just vibes. Since Linux 5.4, the default in this file is 4096; in earlier kernels, the default value is 128. Before Linux 2.4.25, this limit was a hard coded value, SOMAXCONN, with the value 128.
โ ๏ธ no cap but also literally cap โ if you pass a hugebacklog, the kernel just quietly clamps it and you'll never know. check yoursomaxconn, iykyk.
๐ EXAMPLES ๐
See bind(2). yeah the example lives over there, go say hi. ๐ซถ
โญ SEE ALSO โญ
accept(2), bind(2), connect(2), socket(2), socket(7)
okay that's the whole page, hope this was helpful and not confusing, mwah ๐
and for the credits moment: this was the Linux man-pages 6.20 era, dated 2026-02-11, entry listen(2). obsessed with her honestly, she ate. โจ๐๐ฆ
๐ฆ 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
- Copyright 2007, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms