syscalls
listen() — Linux system interface
Mark a connection-oriented socket as passive so incoming connections can be accepted. Bind first for a deliberate local address. Backlog is a queue limit request, not the number of active clients and not a guarantee of effective capacity. Linux caps the value; TCP pending handshake behavior differs from established-connection queues. EOPNOTSUPP can indicate an incompatible socket type. Compile the complete C example in a fresh private working directory using gcc -std=c17 -Wall -Wextra -Wpedantic -Werror example.c -o example; run ./example. POSIX interface; tested on Linux/glibc only. These are libc-facing signatures, not raw architecture-specific syscall ABIs.
Formal Syntax & Structure
linux syntaxlinux
#include <sys/socket.h>int listen(int sockfd, int backlog);Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| sockfd | int | Connection-oriented socket descriptor. |
| backlog | int | Requested pending connection queue limit. |
Return Value
int
0 on success. On failure, returns -1 and sets errno.
Working Example
Example Snippetlinux
#define _GNU_SOURCE#define _POSIX_C_SOURCE 200809L#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/socket.h>#include <sys/un.h>
/* A failed check ends this short-lived demonstration. */#define CHECK(x) do { if (!(x)) { \ fprintf(stderr, "check failed at line %d: %s\n", __LINE__, #x); \ return EXIT_FAILURE; \} } while (0)
int main(void){ struct sockaddr_un a = { .sun_family = AF_UNIX, .sun_path = "demo.sock" }; int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); CHECK(s >= 0); CHECK(bind(s, (struct sockaddr *)&a, sizeof a) == 0); CHECK(listen(s, 4) == 0);
int state = 0; socklen_t len = sizeof state; CHECK(getsockopt(s, SOL_SOCKET, SO_ACCEPTCONN, &state, &len) == 0); CHECK(state != 0); CHECK(close(s) == 0); CHECK(unlink("demo.sock") == 0); puts("SO_ACCEPTCONN confirms listening state"); return EXIT_SUCCESS;}