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 โ€” the socket argument 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 โ€” the socket argument does not refer to a socket. that's not a socket, that's a random int ๐Ÿ“
  • EOPNOTSUPP โ€” the socket protocol does not support listen(). 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> ๐Ÿซถ


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