Skip to content

listen()

Purpose

listen() marks a stream socket as passive, allowing it to accept incoming connections.

#include <sys/types.h>
#include <sys/socket.h>

Prototype

int listen(int sockfd, int backlog);

Minimal Example

if (listen(server_fd, 16) < 0) {
    perror("listen");
    return -1;
}

Common Pitfalls

  • Confusing backlog with the maximum number of active clients.
  • Calling listen() before bind().
  • Not accepting connections quickly enough under load.