LISTEN(3P)
๐ฆ listen(3P) โ POSIX Programmer's Manual ๐ฆ
okay so bestie, this is the manual page for listen(3P), and it lives in the POSIX Programmer's Manual โ yes, the actual POSIX one, not the Linux one, iykyk. buckle up, we're gonna make a socket accept connections ๐
๐ง PROLOG
fr fr, read this first: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so if you're on Linux, go consult the corresponding Linux manual page for the deets on how Linux actually behaves), or โ plot twist โ the interface may not be implemented on Linux at all. so like. vibe check your platform first ๐
โจ NAME โจ
listen โ listen for socket connections and limit the queue of incoming connections ๐
๐ธ SYNOPSIS ๐ธ
#include <sys/socket.h>
int listen(int socket, int backlog);
that's it. two args. we love a minimal signature ๐
๐ DESCRIPTION ๐
so the listen() function shall mark a connection-mode socket, specified by the socket argument, as accepting connections. that's the whole glow-up: your socket goes from "just sitting there" to "main character who takes calls" ๐
the backlog argument is a hint to the implementation, and the implementation shall use it to limit the number of outstanding connections in the socket's listen queue. now โ and this is the part nobody tells you โ implementations may impose a limit on backlog and silently reduce the specified value ๐ญ. so your cute little 128 might lowkey become something else and nobody texts you about it.
normally, a larger backlog argument value shall result in a larger or equal length of the listen queue. and implementations shall support values of backlog up to SOMAXCONN, which is defined in <sys/socket.h>. periodt.
the implementation may include incomplete connections in its listen queue โ and the limits on the number of incomplete connections and completed connections queued may be different. two separate lines, two separate bouncers ๐ซถ
the implementation may also have an upper limit on the length of the listen queue โ either global or per accepting socket. if backlog exceeds this limit, the length of the listen queue is set to the limit. no negotiation, it just caps you ๐คท
if listen() is called with a backlog argument value that is less than 0, the function behaves as if it had been called with a backlog argument value of 0. negative numbers? not a thing here. it's giving zero โจ
a backlog argument of 0 may allow the socket to accept connections, in which case the length of the listen queue may be set to an implementation-defined minimum value. so "zero" doesn't necessarily mean "no". it means "whatever the impl feels like, minimum edition" ๐
and lastly: the socket in use may require the process to have appropriate privileges to use the listen() function. so if it's not working, check your privileges before you check your whole life ๐ฅ
๐ RETURN VALUE ๐
upon successful completions, listen() shall return 0 ๐
otherwise, -1 shall be returned and errno set to indicate the error. classic. the -1 plus errno combo, we know her ๐ญ
๐ ERRORS ๐
the listen() function shall fail if:
EBADFโ thesocketargument is not a valid file descriptor. not a real fd bestie, it's giving ghost ๐ปEDESTADDRREQโ the socket is not bound to a local address, and the protocol does not support listening on an unbound socket. no address, no service ๐EINVALโ the socket is already connected. she's taken. can't be listening if she's already in a relationship ๐ENOTSOCKโ thesocketargument does not refer to a socket. that's not a socket, that's a random int ๐EOPNOTSUPPโ the socket protocol does not supportlisten(). wrong protocol, wrong party, not invited โ
the listen() function may fail if:
EACCESโ the calling process does not have appropriate privileges. permissions said no ๐ซEINVALโ the socket has been shut down. rip, she's gone ๐ENOBUFSโ insufficient resources are available in the system to complete the call. the system is out of vibes, it's broke ๐ญ
the following sections are informative. so like, they're the bonus content, not the binding contract ๐
๐ EXAMPLES ๐
None.
literally none. nothing. empty. the void. ๐ถ
๐ฆ APPLICATION USAGE ๐ฆ
None.
again, nothing. we're thriving on minimalism โจ
โญ RATIONALE โญ
None.
no rationale provided. it just is. iconic behaviour honestly ๐
๐ฎ FUTURE DIRECTIONS ๐ฎ
None.
the future is unspecified and honestly? slay ๐
๐ SEE ALSO ๐
accept()connect()socket()
and from the Base Definitions volume of POSIX.1-2017: <sys_socket.h> ๐ซถ
๐ COPYRIGHT ๐
okay legal bestie moment, here's the whole thing verbatim, no edits:
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie ๐
this one came from the IEEE/The Open Group, dated 2017, and it's page LISTEN(3P) of the POSIX Programmer's Manual. go forth and listen() responsibly ๐ซถโจ๐ธ
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms