1listen(2)                     System Calls Manual                    listen(2)
2
3
4

NAME

6       listen - listen for connections on a socket
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <sys/socket.h>
13
14       int listen(int sockfd, int backlog);
15

DESCRIPTION

17       listen()  marks  the  socket referred to by sockfd as a passive socket,
18       that is, as a socket that will be used to  accept  incoming  connection
19       requests using accept(2).
20
21       The  sockfd  argument  is  a file descriptor that refers to a socket of
22       type SOCK_STREAM or SOCK_SEQPACKET.
23
24       The backlog argument defines the maximum length to which the  queue  of
25       pending  connections  for sockfd may grow.  If a connection request ar‐
26       rives when the queue is full, the client may receive an error  with  an
27       indication  of ECONNREFUSED or, if the underlying protocol supports re‐
28       transmission, the request may be ignored so that a later  reattempt  at
29       connection succeeds.
30

RETURN VALUE

32       On  success,  zero is returned.  On error, -1 is returned, and errno is
33       set to indicate the error.
34

ERRORS

36       EADDRINUSE
37              Another socket is already listening on the same port.
38
39       EADDRINUSE
40              (Internet domain sockets) The socket referred to by  sockfd  had
41              not  previously been bound to an address and, upon attempting to
42              bind it to an ephemeral port, it was determined  that  all  port
43              numbers  in  the ephemeral port range are currently in use.  See
44              the  discussion  of  /proc/sys/net/ipv4/ip_local_port_range   in
45              ip(7).
46
47       EBADF  The argument sockfd is not a valid file descriptor.
48
49       ENOTSOCK
50              The file descriptor sockfd does not refer to a socket.
51
52       EOPNOTSUPP
53              The  socket  is  not of a type that supports the listen() opera‐
54              tion.
55

STANDARDS

57       POSIX.1-2008.
58

HISTORY

60       POSIX.1-2001, 4.4BSD (first appeared in 4.2BSD).
61

NOTES

63       To accept connections, the following steps are performed:
64
65           (1)  A socket is created with socket(2).
66
67           (2)  The socket is bound to a local address using bind(2), so  that
68                other sockets may be connect(2)ed to it.
69
70           (3)  A willingness to accept incoming connections and a queue limit
71                for incoming connections are specified with listen().
72
73           (4)  Connections are accepted with accept(2).
74
75       The behavior of the backlog argument on TCP sockets changed with  Linux
76       2.2.   Now  it  specifies  the  queue length for completely established
77       sockets waiting to be accepted, instead of  the  number  of  incomplete
78       connection  requests.   The  maximum length of the queue for incomplete
79       sockets can be set using /proc/sys/net/ipv4/tcp_max_syn_backlog.   When
80       syncookies are enabled there is no logical maximum length and this set‐
81       ting is ignored.  See tcp(7) for more information.
82
83       If   the   backlog   argument   is   greater   than   the   value    in
84       /proc/sys/net/core/somaxconn, then it is silently capped to that value.
85       Since Linux 5.4, the default in this file is 4096; in earlier  kernels,
86       the  default  value is 128.  Before Linux 2.4.25, this limit was a hard
87       coded value, SOMAXCONN, with the value 128.
88

EXAMPLES

90       See bind(2).
91

SEE ALSO

93       accept(2), bind(2), connect(2), socket(2), socket(7)
94
95
96
97Linux man-pages 6.04              2023-03-30                         listen(2)
Impressum