LISTEN(3POSIX)

πŸ’– listen(3POSIX) β€” the POSIX Programmer's Manual, bestie edition πŸ’–

okay so this is listen(3POSIX), straight out of the POSIX Programmer's Manual, and I am so ready to yap about it.


🫢 PROLOG

Real talk first, because this part matters: 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, bestie, go consult the corresponding Linux manual page for the actual deets on Linux behavior. And lowkey? It's also possible the interface just isn't implemented on Linux at all. No cap. Check before you ship. πŸ’€


✨ NAME ✨

listen β€” listen for socket connections and limit the queue of incoming connections πŸ‘‘


🌸 SYNOPSIS 🌸

#include <sys/socket.h>

int listen(int socket, int backlog);

πŸ’… DESCRIPTION πŸ’…

okay so here's the vibe. The listen() function shall mark a connection-mode socket β€” the one you pass as the socket argument β€” as accepting connections. It's giving "come through, I'm open for business". πŸŽ€

The backlog argument is a hint to the implementation, and the implementation shall use it to limit the number of outstanding connections chillin' in the socket's listen queue. Important: implementations are allowed to impose a limit on backlog and silently reduce the value you gave it. Rude? Maybe. Legal? Yes. Normally, a larger backlog 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>. πŸ“Œ

The implementation may include incomplete connections in its listen queue. And the limits on the number of incomplete connections queued and completed connections queued may be different. Two separate queues, two separate moods. πŸ¦‹

The implementation may have an upper limit on the length of the listen queue β€” and that limit can be global or per accepting socket, dealer's choice. If backlog exceeds this limit, the length of the listen queue is just set to the limit. Cap it and move on. βœ‚οΈ

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 girlies get rounded up to zero, periodt.

A backlog argument of 0 may allow the socket to accept connections, and in that case the length of the listen queue may be set to an implementation-defined minimum value. So zero isn't necessarily "nope", it's "we'll see". πŸ™ˆ

And heads up β€” the socket in use may require the process to have appropriate privileges to use the listen() function. So like... know your permissions. πŸ‘‘


⭐ RETURN VALUE ⭐

Upon successful completions, listen() shall return 0. Otherwise, -1 shall be returned and errno is set to indicate the error. Zero is the slay, negative one is the "something's wrong". 😭


πŸ”₯ ERRORS πŸ”₯

The listen() function shall fail if:

  • EBADF β€” The socket argument is not a valid file descriptor. Not a real one, bestie. πŸ’€
  • EDESTADDRREQ β€” The socket is not bound to a local address, and the protocol does not support listening on an unbound socket. No address, no listening. 🚫
  • EINVAL β€” The socket is already connected. She's taken. πŸ’…
  • ENOTSOCK β€” The socket argument does not refer to a socket. That's just... not a socket. πŸ™ˆ
  • EOPNOTSUPP β€” The socket protocol does not support listen(). Wrong protocol, wrong function, sorry babe. 🫢

The listen() function may fail if:

  • EACCES β€” The calling process does not have appropriate privileges. Permissions check said no. πŸ”’
  • EINVAL β€” The socket has been shut down. She's gone, she's resting. 😴
  • ENOBUFS β€” Insufficient resources are available in the system to complete the call. The system is out of spoons. πŸ“
The following sections are informative. πŸ“’

🌈 EXAMPLES 🌈

None. The examples are simply not here. Iconic behavior, honestly.


🧚 APPLICATION USAGE 🧚

None. Also empty. The page said what it said and dipped.


πŸ’œ RATIONALE πŸ’œ

None. No rationale, no explanation arc, no lore drop.


πŸ¦‹ FUTURE DIRECTIONS πŸ¦‹

None. The future is unspecified and honestly that's very on brand.


πŸ‘‘ SEE ALSO πŸ‘‘

  • accept()
  • connect()
  • socket()

The Base Definitions volume of POSIX.1‐2017, <sys_socket.h> ⭐


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 .


okay that's the whole page, bestie, and that's a wrap on listen(3POSIX) from the POSIX Programmer's Manual β€” brought to you by IEEE/The Open Group, 2017 πŸ’–βœ¨ slay, go bind your sockets, and don't forget to check your backlog πŸŽ€πŸ‘‘