LISTEN(2)
okay so bestie, sit down, grab a boba π§ and let me tell you about LISTEN(2) β that's the page from the System Calls Manual, and it's literally the glow-up your sockets need before they can make friends. we're doing this. π
π NAME π
listen β listen for connections on a socket π¦
(yes that's the whole vibe: your socket is out here waiting for people to slide into its DMs, no cap)
π LIBRARY π
Standard C Library (libc, -lc) π
β¨ SYNOPSIS β¨
#include <sys/socket.h>
int
listen(int s, int backlog);
πΈ DESCRIPTION πΈ
so here's the whole plot, bestie. to accept connections, you first create a socket with socket(2), then you declare a willingness to accept incoming connections and set a queue limit for incoming connections using listen(), and then you actually accept the connections with accept(2). it's a whole three-step era and you cannot skip a step, i don't make the rules. π
the listen() system call applies only to sockets of type SOCK_STREAM or SOCK_SEQPACKET. like, if your socket is a different type, this is simply not its moment. π
the backlog argument defines the maximum length the queue of pending connections may grow to. plot twist though β the real maximum queue length will be 1.5 times more than the value specified in the backlog argument. math is math. β¨
a subsequent listen() system call on the listening socket allows the caller to change the maximum queue length using a new backlog argument. glow-up on demand, honestly.
if a connection request arrives with the queue full, the client may receive an error with an indication of ECONNREFUSED, or, in the case of TCP, the connection will be silently dropped. silently. dropped. ghosted, literally ghosted, no explanation, no text back. π»
current queue lengths of listening sockets can be queried using the netstat(1) command. π
note that before FreeBSD 4.5 and the introduction of the syncache, the backlog argument also determined the length of the incomplete connection queue, which held TCP sockets in the process of completing TCP's 3-way handshake. these incomplete connections are now held entirely in the syncache, which is unaffected by queue lengths. so inflating backlog values to help handle denial of service attacks? no longer necessary. we love an upgrade. π«Ά
the sysctl(3) MIB variable kern.ipc.soacceptqueue specifies a hard limit on backlog; if a value greater than kern.ipc.soacceptqueue or less than zero is specified, backlog is silently forced to kern.ipc.soacceptqueue. the kernel really said "no thoughts, just the limit." π
π INTERACTION WITH ACCEPT FILTERS π
when accept filtering is used on a socket, a second queue will be used to hold sockets that have connected, but have not yet met their accept filtering criteria. once the criteria has been met, these sockets will be moved over into the completed connection queue to be accept(2)ed. moving up in the world, we love to see it. π
but β if this secondary queue is full and a new connection comes in, the oldest socket which has not yet met its accept filter criteria will be terminated. terminated. first in, first out, and out means out. π
this secondary queue, like the primary listen queue, is sized according to the backlog argument. everything is backlog, it's giving main character energy fr fr. π
π RETURN VALUES π
the listen() function returns the value 0 if successful; otherwise, the value -1 is returned and the global variable errno is set to indicate the error.
so: 0 = slay β¨, -1 = go read your errno bestie π«Ά
π ERRORS π
the listen() system call will fail if:
[EBADF]β the argumentsis not a valid descriptor. (that's not a real fd, babes π)[EDESTADDRREQ]β the socket is not bound to a local address, and the protocol does not support listening on an unbound socket. (nowhere to be found, no address, no service π)[EINVAL]β the socket is already connected, or in the process of being connected. (it's already taken, sorry π )[ENOTSOCK]β the argumentsis not a socket. (you brought the wrong thing to the party π)[EOPNOTSUPP]β the socket is not of a type that supports the operationlisten(). (this type simply does not dolisten(), and that's that π)
π SEE ALSO π
netstat(1), accept(2), connect(2), socket(2), sysctl(3), sysctl(8), accept_filter(9)
go read them, they're all connected, iykyk. π₯
π°οΈ HISTORY π°οΈ
the listen() system call appeared in 4.2BSD. the ability to configure the maximum backlog at run-time, and to use a negative backlog to request the maximum allowable value, was introduced in FreeBSD 2.2. the kern.ipc.somaxconn sysctl(3) has been replaced with kern.ipc.soacceptqueue in FreeBSD 10.0 to prevent confusion about its actual functionality. and the original sysctl(3) kern.ipc.somaxconn is still available but hidden from a sysctl(3) -a output so that existing applications and scripts continue to work. backwards compatible and lowkey iconic. π«Ά
and that's the whole page, bestie!! this one came out of Debian, dated August 18, 2016 β so go forth, listen() responsibly, and remember: if the queue is full, somebody's getting ghosted. mwah ππ
π¦ 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