1LISTEN(2) Linux Programmer's Manual LISTEN(2)
2
3
4
6 listen - listen for connections on a socket
7
9 #include <sys/socket.h>
10
11 int listen(int sockfd, int backlog);
12
14 listen() marks the socket referred to by sockfd as a passive socket,
15 that is, as a socket that will be used to accept incoming connection
16 requests using accept(2).
17
18 The sockfd argument is a file descriptor that refers to a socket of
19 type SOCK_STREAM or SOCK_SEQPACKET.
20
21 The backlog argument defines the maximum length to which the queue of
22 pending connections for sockfd may grow. If a connection request ar‐
23 rives when the queue is full, the client may receive an error with an
24 indication of ECONNREFUSED or, if the underlying protocol supports re‐
25 transmission, the request may be ignored so that a later reattempt at
26 connection succeeds.
27
29 On success, zero is returned. On error, -1 is returned, and errno is
30 set to indicate the error.
31
33 EADDRINUSE
34 Another socket is already listening on the same port.
35
36 EADDRINUSE
37 (Internet domain sockets) The socket referred to by sockfd had
38 not previously been bound to an address and, upon attempting to
39 bind it to an ephemeral port, it was determined that all port
40 numbers in the ephemeral port range are currently in use. See
41 the discussion of /proc/sys/net/ipv4/ip_local_port_range in
42 ip(7).
43
44 EBADF The argument sockfd is not a valid file descriptor.
45
46 ENOTSOCK
47 The file descriptor sockfd does not refer to a socket.
48
49 EOPNOTSUPP
50 The socket is not of a type that supports the listen() opera‐
51 tion.
52
54 POSIX.1-2001, POSIX.1-2008, 4.4BSD (listen() first appeared in 4.2BSD).
55
57 To accept connections, the following steps are performed:
58
59 1. A socket is created with socket(2).
60
61 2. The socket is bound to a local address using bind(2), so that
62 other sockets may be connect(2)ed to it.
63
64 3. A willingness to accept incoming connections and a queue limit
65 for incoming connections are specified with listen().
66
67 4. Connections are accepted with accept(2).
68
69 The behavior of the backlog argument on TCP sockets changed with Linux
70 2.2. Now it specifies the queue length for completely established
71 sockets waiting to be accepted, instead of the number of incomplete
72 connection requests. The maximum length of the queue for incomplete
73 sockets can be set using /proc/sys/net/ipv4/tcp_max_syn_backlog. When
74 syncookies are enabled there is no logical maximum length and this set‐
75 ting is ignored. See tcp(7) for more information.
76
77 If the backlog argument is greater than the value in
78 /proc/sys/net/core/somaxconn, then it is silently capped to that value.
79 Since Linux 5.4, the default in this file is 4096; in earlier kernels,
80 the default value is 128. In kernels before 2.4.25, this limit was a
81 hard coded value, SOMAXCONN, with the value 128.
82
84 See bind(2).
85
87 accept(2), bind(2), connect(2), socket(2), socket(7)
88
90 This page is part of release 5.13 of the Linux man-pages project. A
91 description of the project, information about reporting bugs, and the
92 latest version of this page, can be found at
93 https://www.kernel.org/doc/man-pages/.
94
95
96
97Linux 2021-03-22 LISTEN(2)