1 .\" $OpenBSD: accept.2,v 1.28 2014/09/09 03:50:43 guenther Exp $
2 .\" $NetBSD: accept.2,v 1.7 1996/01/31 20:14:42 mycroft Exp $
4 .\" Copyright (c) 1983, 1990, 1991, 1993
5 .\" The Regents of the University of California. All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. Neither the name of the University nor the names of its contributors
16 .\" may be used to endorse or promote products derived from this software
17 .\" without specific prior written permission.
19 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" @(#)accept.2 8.2 (Berkeley) 12/11/93
39 .Nd accept a connection on a socket
43 .Fn accept "int s" "struct sockaddr *addr" "socklen_t *addrlen"
45 .Fn accept4 "int s" "struct sockaddr *addr" "socklen_t *addrlen" "int flags"
49 is a socket that has been created with
51 bound to an address with
53 and is listening for connections after a
57 call extracts the first connection request on the queue of pending
58 connections, creates a new socket with the same non-blocking I/O mode as
60 and allocates a new file descriptor for the socket with the
61 close-on-exec flag clear.
65 system call is similar, however the non-blocking I/O mode of the
66 new socket is determined by the
72 argument and the close-on-exec flag on the new file descriptor is
86 If no pending connections are present on the queue,
87 and the socket is not marked as non-blocking,
89 blocks the caller until a connection is present.
90 If the socket is marked non-blocking and no pending
91 connections are present on the queue,
93 returns an error as described below.
94 The accepted socket may not be used to accept more connections.
101 is a result parameter that is filled in with the address of the connecting
102 entity as known to the communications layer.
103 The exact format of the
105 parameter is determined by the domain in which the communication
109 exists for greater portability.
110 It is large enough to hold any of the types that may be returned in the
116 is a value-result parameter; it should initially contain the
117 amount of space pointed to by
119 on return it will contain the actual length (in bytes) of the
123 does not point to enough space to hold the entire socket address, the
124 result will be truncated to the initial value of
127 This call is used with connection-based socket types, currently with
134 a socket for the purposes of doing an
136 by selecting it for read.
138 The call returns \-1 on error.
139 If it succeeds, it returns a non-negative integer that is a descriptor
140 for the accepted socket.
142 The following code uses struct
144 to allocate enough space for the returned address:
145 .Bd -literal -offset indent
146 #include <sys/types.h>
147 #include <sys/socket.h>
149 struct sockaddr_storage addr;
150 socklen_t len = sizeof(addr);
153 retcode = accept(s, (struct sockaddr *)&addr, &len);
164 The descriptor is invalid.
166 The descriptor doesn't reference a socket.
168 The referenced socket is not of type
171 A signal was caught before a connection arrived.
173 The referenced socket is not listening for connections (that is,
175 has not yet been called).
181 parameter is not in a valid part of the process address space.
182 .It Bq Er EWOULDBLOCK
183 The socket is marked non-blocking and no connections
184 are present to be accepted.
186 The per-process descriptor table is full.
187 .It Bq Er ECONNABORTED
188 A connection has been aborted.
190 There was insufficient memory available to complete the operation.
215 function is expected to conform to a future revision of that standard.
219 system call first appeared in