OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / 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 2014-05-10 "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 .br
52 .B #include <sys/socket.h>
53 .sp
54 .BI "int listen(int " sockfd ", int " backlog );
55 .fi
56 .SH DESCRIPTION
57 .BR listen ()
58 marks the socket referred to by
59 .I sockfd
60 as a passive socket, that is, as a socket that will
61 be used to accept incoming connection requests using
62 .BR accept (2).
63
64 The
65 .I sockfd
66 argument is a file descriptor that refers to a socket of type
67 .B SOCK_STREAM
68 or
69 .BR SOCK_SEQPACKET .
70
71 The
72 .I backlog
73 argument defines the maximum length
74 to which the queue of pending connections for
75 .I sockfd
76 may grow.
77 If a connection request arrives when the queue is full, the client
78 may receive an error with an indication of
79 .B ECONNREFUSED
80 or, if the underlying protocol supports retransmission, the request may be
81 ignored so that a later reattempt at connection succeeds.
82 .SH RETURN VALUE
83 On success, zero is returned.
84 On error, \-1 is returned, and
85 .I errno
86 is set appropriately.
87 .SH ERRORS
88 .TP
89 .B EADDRINUSE
90 Another socket is already listening on the same port.
91 .TP
92 .B EADDRINUSE
93 (Internet domain sockets)
94 The socket referred to by
95 .I sockfd
96 had not previously been bound to an address and,
97 upon attempting to bind it to an ephemeral port,
98 it was determined that all port numbers in the ephemeral port range
99 are currently in use.
100 See the discussion of
101 .I /proc/sys/net/ipv4/ip_local_port_range
102 in
103 .BR ip (7).
104 .TP
105 .B EBADF
106 The argument
107 .I sockfd
108 is not a valid descriptor.
109 .TP
110 .B ENOTSOCK
111 The argument
112 .I sockfd
113 is not a socket.
114 .TP
115 .B EOPNOTSUPP
116 The socket is not of a type that supports the
117 .BR listen ()
118 operation.
119 .SH CONFORMING TO
120 4.4BSD, POSIX.1-2001.
121 The
122 .BR listen ()
123 function call first appeared in 4.2BSD.
124 .SH NOTES
125 To accept connections, the following steps are performed:
126 .RS 4
127 .IP 1. 4
128 A socket is created with
129 .BR socket (2).
130 .IP 2.
131 The socket is bound to a local address using
132 .BR bind (2),
133 so that other sockets may be
134 .BR connect (2)ed
135 to it.
136 .IP 3.
137 A willingness to accept incoming connections and a queue limit for incoming
138 connections are specified with
139 .BR listen ().
140 .IP 4.
141 Connections are accepted with
142 .BR accept (2).
143 .RE
144 .PP
145 POSIX.1-2001 does not require the inclusion of
146 .IR <sys/types.h> ,
147 and this header file is not required on Linux.
148 However, some historical (BSD) implementations required this header
149 file, and portable applications are probably wise to include it.
150
151 The behavior of the
152 .I backlog
153 argument on TCP sockets changed with Linux 2.2.
154 Now it specifies the queue length for
155 .I completely
156 established sockets waiting to be accepted,
157 instead of the number of incomplete connection requests.
158 The maximum length of the queue for incomplete sockets
159 can be set using
160 .IR /proc/sys/net/ipv4/tcp_max_syn_backlog .
161 When syncookies are enabled there is no logical maximum
162 length and this setting is ignored.
163 See
164 .BR tcp (7)
165 for more information.
166
167 If the
168 .I backlog
169 argument is greater than the value in
170 .IR /proc/sys/net/core/somaxconn ,
171 then it is silently truncated to that value;
172 the default value in this file is 128.
173 In kernels before 2.4.25, this limit was a hard coded value,
174 .BR SOMAXCONN ,
175 with the value 128.
176 .\" The following is now rather historic information (MTK, Jun 05)
177 .\" Don't rely on this value in portable applications since BSD
178 .\" (and some BSD-derived systems) limit the backlog to 5.
179 .SH EXAMPLE
180 See
181 .BR bind (2).
182 .SH SEE ALSO
183 .BR accept (2),
184 .BR bind (2),
185 .BR connect (2),
186 .BR socket (2),
187 .BR socket (7)
188 .SH COLOPHON
189 This page is part of release 3.68 of the Linux
190 .I man-pages
191 project.
192 A description of the project,
193 information about reporting bugs,
194 and the latest version of this page,
195 can be found at
196 \%http://www.kernel.org/doc/man\-pages/.