1 .\" Copyright (c) 1983, 1990, 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" SPDX-License-Identifier: BSD-4-Clause-UC
6 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
7 .\" Modified 1996-10-21 by Eric S. Raymond <esr@thyrsus.com>
8 .\" Modified 1998-2000 by Andi Kleen to match Linux 2.2 reality
9 .\" Modified 2002-04-23 by Roger Luethi <rl@hellgate.ch>
10 .\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
11 .\" 2008-12-04, mtk, Add documentation of accept4()
13 .TH ACCEPT 2 2022-09-09 "Linux man-pages (unreleased)"
15 accept, accept4 \- accept a connection on a socket
18 .RI ( libc ", " \-lc )
21 .B #include <sys/socket.h>
23 .BI "int accept(int " sockfd ", struct sockaddr *restrict " addr ,
24 .BI " socklen_t *restrict " addrlen );
26 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
27 .B #include <sys/socket.h>
29 .BI "int accept4(int " sockfd ", struct sockaddr *restrict " addr ,
30 .BI " socklen_t *restrict " addrlen ", int " flags );
35 system call is used with connection-based socket types
38 It extracts the first connection request on the queue of pending
39 connections for the listening socket,
41 creates a new connected socket, and returns a new file
42 descriptor referring to that socket.
43 The newly created socket is not in the listening state.
46 is unaffected by this call.
50 is a socket that has been created with
52 bound to a local address with
54 and is listening for connections after a
62 This structure is filled in with the address of the peer socket,
63 as known to the communications layer.
64 The exact format of the address returned
66 is determined by the socket's address family (see
68 and the respective protocol man pages).
71 is NULL, nothing is filled in; in this case,
73 is not used, and should also be NULL.
77 argument is a value-result argument:
78 the caller must initialize it to contain the
79 size (in bytes) of the structure pointed to by
81 on return it will contain the actual size of the peer address.
83 The returned address is truncated if the buffer provided is too small;
86 will return a value greater than was supplied to the call.
89 connections are present on the queue, and the socket is not marked as
92 blocks the caller until a connection is present.
93 If the socket is marked
94 nonblocking and no pending connections are present on the queue,
101 In order to be notified of incoming connections on a socket, you can use
106 A readable event will be delivered when a new connection is attempted and you
109 to get a socket for that connection.
110 Alternatively, you can set the socket to deliver
112 when activity occurs on a socket; see
122 The following values can be bitwise ORed in
124 to obtain different behavior:
129 file status flag on the open file description (see
131 referred to by the new file descriptor.
132 Using this flag saves extra calls to
134 to achieve the same result.
137 Set the close-on-exec
139 flag on the new file descriptor.
140 See the description of the
144 for reasons why this may be useful.
147 these system calls return a file descriptor
148 for the accepted socket (a nonnegative integer).
149 On error, \-1 is returned,
151 is set to indicate the error, and
159 passes already-pending network errors on the new socket
160 as an error code from
162 This behavior differs from other BSD socket
164 For reliable operation the application should detect
165 the network errors defined for the protocol after
171 In the case of TCP/IP, these are
183 .BR EAGAIN " or " EWOULDBLOCK
184 .\" Actually EAGAIN on Linux
185 The socket is marked nonblocking and no connections are
186 present to be accepted.
187 POSIX.1-2001 and POSIX.1-2008
188 allow either error to be returned for this case,
189 and do not require these constants to have the same value,
190 so a portable application should check for both possibilities.
194 is not an open file descriptor.
197 A connection has been aborted.
202 argument is not in a writable part of the user address space.
205 The system call was interrupted by a signal that was caught
206 before a valid connection arrived; see
210 Socket is not listening for connections, or
212 is invalid (e.g., is negative).
220 The per-process limit on the number of open file descriptors has been reached.
223 The system-wide limit on the total number of open files has been reached.
225 .BR ENOBUFS ", " ENOMEM
226 Not enough free memory.
227 This often means that the memory allocation is limited by the socket buffer
228 limits, not by the system memory.
233 does not refer to a socket.
236 The referenced socket is not of type
240 Firewall rules forbid connection.
245 In addition, network errors for the new socket and as defined
246 for the protocol may be returned.
247 Various Linux kernels can
248 return other errors such as
250 .BR ESOCKTNOSUPPORT ,
251 .BR EPROTONOSUPPORT ,
255 may be seen during a trace.
259 system call is available starting with Linux 2.6.28;
260 support in glibc is available starting with version 2.10.
263 POSIX.1-2001, POSIX.1-2008,
266 first appeared in 4.2BSD).
267 .\" The BSD man page documents five possible error returns
268 .\" (EBADF, ENOTSOCK, EOPNOTSUPP, EWOULDBLOCK, EFAULT).
269 .\" POSIX.1-2001 documents errors
270 .\" EAGAIN, EBADF, ECONNABORTED, EINTR, EINVAL, EMFILE,
271 .\" ENFILE, ENOBUFS, ENOMEM, ENOTSOCK, EOPNOTSUPP, EPROTO, EWOULDBLOCK.
272 .\" In addition, SUSv2 documents EFAULT and ENOSR.
275 is a nonstandard Linux extension.
277 On Linux, the new socket returned by
279 does \fInot\fP inherit file status flags such as
283 from the listening socket.
284 This behavior differs from the canonical BSD sockets implementation.
285 .\" Some testing seems to show that Tru64 5.1 and HP-UX 11 also
286 .\" do not inherit file status flags -- MTK Jun 05
287 Portable programs should not rely on inheritance or noninheritance
288 of file status flags and always explicitly set all required flags on
289 the socket returned from
292 There may not always be a connection waiting after a
299 return a readability event because the connection might have been
300 removed by an asynchronous network error or another thread before
303 If this happens, then the call will block waiting for the next
304 connection to arrive.
307 never blocks, the passed socket
314 For certain protocols which require an explicit confirmation,
317 can be thought of as merely dequeuing the next connection request and not
318 implying confirmation.
319 Confirmation can be implied by
320 a normal read or write on the new file descriptor, and rejection can be
321 implied by closing the new socket.
322 Currently, only DECnet has these semantics on Linux.
324 .SS The socklen_t type
325 In the original BSD sockets implementation (and on other older systems)
326 .\" such as Linux libc4 and libc5, SunOS 4, SGI
327 the third argument of
329 was declared as an \fIint\ *\fP.
331 standard wanted to change it into a \fIsize_t\ *\fPC;
332 .\" SunOS 5 has 'size_t *'
333 later POSIX standards and glibc 2.x have