]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/listen.2
mknod.2: tfix
[thirdparty/man-pages.git] / man2 / listen.2
1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" and Copyright (C) 2007, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" All rights reserved.
4 .\"
5 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
21 .\"
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\" %%%LICENSE_END
34 .\"
35 .\" $Id: listen.2,v 1.6 1999/05/18 14:10:32 freitag Exp $
36 .\"
37 .\" Modified Fri Jul 23 22:07:54 1993 by Rik Faith <faith@cs.unc.edu>
38 .\" Modified 950727 by aeb, following a suggestion by Urs Thuermann
39 .\" <urs@isnogud.escape.de>
40 .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
41 .\" Modified 1998 by Andi Kleen
42 .\" Modified 11 May 2001 by Sam Varshavchik <mrsam@courier-mta.com>
43 .\"
44 .\"
45 .TH LISTEN 2 2020-02-09 "Linux" "Linux Programmer's Manual"
46 .SH NAME
47 listen \- listen for connections on a socket
48 .SH SYNOPSIS
49 .nf
50 .BR "#include <sys/types.h>" " /* See NOTES */"
51 .B #include <sys/socket.h>
52 .PP
53 .BI "int listen(int " sockfd ", int " backlog );
54 .fi
55 .SH DESCRIPTION
56 .BR listen ()
57 marks the socket referred to by
58 .I sockfd
59 as a passive socket, that is, as a socket that will
60 be used to accept incoming connection requests using
61 .BR accept (2).
62 .PP
63 The
64 .I sockfd
65 argument is a file descriptor that refers to a socket of type
66 .B SOCK_STREAM
67 or
68 .BR SOCK_SEQPACKET .
69 .PP
70 The
71 .I backlog
72 argument defines the maximum length
73 to which the queue of pending connections for
74 .I sockfd
75 may grow.
76 If a connection request arrives when the queue is full, the client
77 may receive an error with an indication of
78 .B ECONNREFUSED
79 or, if the underlying protocol supports retransmission, the request may be
80 ignored so that a later reattempt at connection succeeds.
81 .SH RETURN VALUE
82 On success, zero is returned.
83 On error, \-1 is returned, and
84 .I errno
85 is set appropriately.
86 .SH ERRORS
87 .TP
88 .B EADDRINUSE
89 Another socket is already listening on the same port.
90 .TP
91 .B EADDRINUSE
92 (Internet domain sockets)
93 The socket referred to by
94 .I sockfd
95 had not previously been bound to an address and,
96 upon attempting to bind it to an ephemeral port,
97 it was determined that all port numbers in the ephemeral port range
98 are currently in use.
99 See the discussion of
100 .I /proc/sys/net/ipv4/ip_local_port_range
101 in
102 .BR ip (7).
103 .TP
104 .B EBADF
105 The argument
106 .I sockfd
107 is not a valid file descriptor.
108 .TP
109 .B ENOTSOCK
110 The file descriptor
111 .I sockfd
112 does not refer to a socket.
113 .TP
114 .B EOPNOTSUPP
115 The socket is not of a type that supports the
116 .BR listen ()
117 operation.
118 .SH CONFORMING TO
119 POSIX.1-2001, POSIX.1-2008, 4.4BSD
120 .RB ( listen ()
121 first appeared in 4.2BSD).
122 .SH NOTES
123 To accept connections, the following steps are performed:
124 .RS 4
125 .IP 1. 4
126 A socket is created with
127 .BR socket (2).
128 .IP 2.
129 The socket is bound to a local address using
130 .BR bind (2),
131 so that other sockets may be
132 .BR connect (2)ed
133 to it.
134 .IP 3.
135 A willingness to accept incoming connections and a queue limit for incoming
136 connections are specified with
137 .BR listen ().
138 .IP 4.
139 Connections are accepted with
140 .BR accept (2).
141 .RE
142 .PP
143 POSIX.1 does not require the inclusion of
144 .IR <sys/types.h> ,
145 and this header file is not required on Linux.
146 However, some historical (BSD) implementations required this header
147 file, and portable applications are probably wise to include it.
148 .PP
149 The behavior of the
150 .I backlog
151 argument on TCP sockets changed with Linux 2.2.
152 Now it specifies the queue length for
153 .I completely
154 established sockets waiting to be accepted,
155 instead of the number of incomplete connection requests.
156 The maximum length of the queue for incomplete sockets
157 can be set using
158 .IR /proc/sys/net/ipv4/tcp_max_syn_backlog .
159 When syncookies are enabled there is no logical maximum
160 length and this setting is ignored.
161 See
162 .BR tcp (7)
163 for more information.
164 .PP
165 If the
166 .I backlog
167 argument is greater than the value in
168 .IR /proc/sys/net/core/somaxconn ,
169 then it is silently truncated to that value.
170 Since Linux 5.4, the default in this file is 4096;
171 in earlier kernels, the default value is 128.
172 In kernels before 2.4.25, this limit was a hard coded value,
173 .BR SOMAXCONN ,
174 with the value 128.
175 .\" The following is now rather historic information (MTK, Jun 05)
176 .\" Don't rely on this value in portable applications since BSD
177 .\" (and some BSD-derived systems) limit the backlog to 5.
178 .SH EXAMPLE
179 See
180 .BR bind (2).
181 .SH SEE ALSO
182 .BR accept (2),
183 .BR bind (2),
184 .BR connect (2),
185 .BR socket (2),
186 .BR socket (7)