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 by sockfd had 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_range in ip(7). yeah this one hits twice, same name, different lore โ€” that's on purpose, don't come for me.
  • EBADF โ€” The argument sockfd is not a valid file descriptor. not a real fd, blocked. ๐Ÿ’€
  • ENOTSOCK โ€” The file descriptor sockfd does 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 the listen() 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:

  1. A socket is created with socket(2).
  2. The socket is bound to a local address using bind(2), so that other sockets may be connect(2)ed to it.
  3. A willingness to accept incoming connections and a queue limit for incoming connections are specified with listen().
  4. 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 huge backlog, the kernel just quietly clamps it and you'll never know. check your somaxconn, 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms