1LISTEN(2) Linux Programmer's Manual LISTEN(2)
2
3
4
6 listen - listen for connections on a socket
7
9 #include <sys/types.h> /* See NOTES */
10 #include <sys/socket.h>
11
12 int listen(int sockfd, int backlog);
13
15 listen() marks the socket referred to by sockfd as a passive socket,
16 that is, as a socket that will be used to accept incoming connection
17 requests using accept(2).
18
19 The sockfd argument is a file descriptor that refers to a socket of
20 type SOCK_STREAM or SOCK_SEQPACKET.
21
22 The backlog argument defines the maximum length to which the queue of
23 pending connections for sockfd may grow. If a connection request
24 arrives when the queue is full, the client may receive an error with an
25 indication of ECONNREFUSED or, if the underlying protocol supports
26 retransmission, the request may be ignored so that a later reattempt at
27 connection succeeds.
28
30 On success, zero is returned. On error, -1 is returned, and errno is
31 set appropriately.
32
34 EADDRINUSE
35 Another socket is already listening on the same port.
36
37 EADDRINUSE
38 (Internet domain sockets) The socket referred to by sockfd had
39 not previously been bound to an address and, upon attempting to
40 bind it to an ephemeral port, it was determined that all port
41 numbers in the ephemeral port range are currently in use. See
42 the discussion of /proc/sys/net/ipv4/ip_local_port_range in
43 ip(7).
44
45 EBADF The argument sockfd is not a valid file descriptor.
46
47 ENOTSOCK
48 The file descriptor sockfd does not refer to a socket.
49
50 EOPNOTSUPP
51 The socket is not of a type that supports the listen() opera‐
52 tion.
53
55 POSIX.1-2001, POSIX.1-2008, 4.4BSD (listen() first appeared in 4.2BSD).
56
58 To accept connections, the following steps are performed:
59
60 1. A socket is created with socket(2).
61
62 2. The socket is bound to a local address using bind(2), so that
63 other sockets may be connect(2)ed to it.
64
65 3. A willingness to accept incoming connections and a queue limit
66 for incoming connections are specified with listen().
67
68 4. Connections are accepted with accept(2).
69
70 POSIX.1 does not require the inclusion of <sys/types.h>, and this
71 header file is not required on Linux. However, some historical (BSD)
72 implementations required this header file, and portable applications
73 are probably wise to include it.
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 truncated to that
85 value; the default value in this file is 128. In kernels before
86 2.4.25, this limit was a hard coded value, SOMAXCONN, with the value
87 128.
88
90 See bind(2).
91
93 accept(2), bind(2), connect(2), socket(2), socket(7)
94
96 This page is part of release 5.02 of the Linux man-pages project. A
97 description of the project, information about reporting bugs, and the
98 latest version of this page, can be found at
99 https://www.kernel.org/doc/man-pages/.
100
101
102
103Linux 2017-09-15 LISTEN(2)