1ACCEPT(2)                  Linux Programmer's Manual                 ACCEPT(2)
2
3
4

NAME

6       accept - accept a connection on a socket
7

SYNOPSIS

9       #include <sys/types.h>          /* See NOTES */
10       #include <sys/socket.h>
11
12       int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
13
14       #define _GNU_SOURCE
15       #include <sys/socket.h>
16
17       int accept4(int sockfd, struct sockaddr *addr,
18                   socklen_t *addrlen, int flags);
19

DESCRIPTION

21       The  accept()  system  call  is used with connection-based socket types
22       (SOCK_STREAM,  SOCK_SEQPACKET).   It  extracts  the  first   connection
23       request  on  the queue of pending connections for the listening socket,
24       sockfd, creates a new connected socket, and returns a new file descrip‐
25       tor  referring  to that socket.  The newly created socket is not in the
26       listening state.  The original socket  sockfd  is  unaffected  by  this
27       call.
28
29       The  argument  sockfd is a socket that has been created with socket(2),
30       bound to a local address with bind(2), and is listening for connections
31       after a listen(2).
32
33       The argument addr is a pointer to a sockaddr structure.  This structure
34       is filled in with the address of the peer socket, as known to the  com‐
35       munications  layer.   The  exact format of the address returned addr is
36       determined by the  socket's  address  family  (see  socket(2)  and  the
37       respective  protocol  man pages).  When addr is NULL, nothing is filled
38       in; in this case, addrlen is not used, and should also be NULL.
39
40       The addrlen argument is a value-result argument: the caller  must  ini‐
41       tialize  it  to contain the size (in bytes) of the structure pointed to
42       by addr; on return it will contain the actual size of the peer address.
43
44       The returned address is truncated if the buffer provided is too  small;
45       in  this case, addrlen will return a value greater than was supplied to
46       the call.
47
48       If no pending connections are present on the queue, and the  socket  is
49       not  marked  as nonblocking, accept() blocks the caller until a connec‐
50       tion is present.  If the socket is marked nonblocking  and  no  pending
51       connections  are  present  on  the queue, accept() fails with the error
52       EAGAIN or EWOULDBLOCK.
53
54       In order to be notified of incoming connections on a  socket,  you  can
55       use  select(2)  or  poll(2).  A readable event will be delivered when a
56       new connection is attempted and you may then call  accept()  to  get  a
57       socket  for  that connection.  Alternatively, you can set the socket to
58       deliver SIGIO when activity occurs  on  a  socket;  see  socket(7)  for
59       details.
60
61       For  certain  protocols which require an explicit confirmation, such as
62       DECNet, accept() can be thought of as merely dequeuing the next connec‐
63       tion  request  and  not  implying  confirmation.   Confirmation  can be
64       implied by a normal read or write  on  the  new  file  descriptor,  and
65       rejection  can  be  implied  by closing the new socket.  Currently only
66       DECNet has these semantics on Linux.
67
68       If flags is 0, then accept4() is the same as accept().   The  following
69       values can be bitwise ORed in flags to obtain different behavior:
70
71       SOCK_NONBLOCK   Set  the  O_NONBLOCK  file  status flag on the new open
72                       file description.  Using this flag saves extra calls to
73                       fcntl(2) to achieve the same result.
74
75       SOCK_CLOEXEC    Set the close-on-exec (FD_CLOEXEC) flag on the new file
76                       descriptor.  See the description of the O_CLOEXEC  flag
77                       in open(2) for reasons why this may be useful.
78

RETURN VALUE

80       On  success,  these system calls return a nonnegative integer that is a
81       descriptor for the accepted socket.  On  error,  -1  is  returned,  and
82       errno is set appropriately.
83
84   Error Handling
85       Linux accept() (and accept4()) passes already-pending network errors on
86       the new socket as an error code from accept().  This  behavior  differs
87       from  other  BSD  socket  implementations.   For reliable operation the
88       application should detect the network errors defined for  the  protocol
89       after  accept()  and  treat  them  like EAGAIN by retrying.  In case of
90       TCP/IP these are  ENETDOWN,  EPROTO,  ENOPROTOOPT,  EHOSTDOWN,  ENONET,
91       EHOSTUNREACH, EOPNOTSUPP, and ENETUNREACH.
92

ERRORS

94       EAGAIN or EWOULDBLOCK
95              The  socket is marked nonblocking and no connections are present
96              to be accepted.  POSIX.1-2001 allows either error to be returned
97              for  this case, and does not require these constants to have the
98              same value, so a portable application should check for both pos‐
99              sibilities.
100
101       EBADF  The descriptor is invalid.
102
103       ECONNABORTED
104              A connection has been aborted.
105
106       EFAULT The  addr argument is not in a writable part of the user address
107              space.
108
109       EINTR  The system call was interrupted by  a  signal  that  was  caught
110              before a valid connection arrived; see signal(7).
111
112       EINVAL Socket  is  not listening for connections, or addrlen is invalid
113              (e.g., is negative).
114
115       EINVAL (accept4()) invalid value in flags.
116
117       EMFILE The per-process limit of open file descriptors has been reached.
118
119       ENFILE The system limit on the total number  of  open  files  has  been
120              reached.
121
122       ENOBUFS, ENOMEM
123              Not  enough free memory.  This often means that the memory allo‐
124              cation is limited by the socket buffer limits, not by the system
125              memory.
126
127       ENOTSOCK
128              The descriptor references a file, not a socket.
129
130       EOPNOTSUPP
131              The referenced socket is not of type SOCK_STREAM.
132
133       EPROTO Protocol error.
134
135       In addition, Linux accept() may fail if:
136
137       EPERM  Firewall rules forbid connection.
138
139       In  addition,  network errors for the new socket and as defined for the
140       protocol may be returned.   Various  Linux  kernels  can  return  other
141       errors such as ENOSR, ESOCKTNOSUPPORT, EPROTONOSUPPORT, ETIMEDOUT.  The
142       value ERESTARTSYS may be seen during a trace.
143

VERSIONS

145       The accept4() system call is available starting with Linux 2.6.28; sup‐
146       port in glibc is available starting with version 2.10.
147

CONFORMING TO

149       accept():  POSIX.1-2001,  SVr4,  4.4BSD,  (accept()  first  appeared in
150       4.2BSD).
151
152       accept4() is a nonstandard Linux extension.
153
154       On Linux, the new socket returned by accept()  does  not  inherit  file
155       status  flags such as O_NONBLOCK and O_ASYNC from the listening socket.
156       This behavior differs from the canonical  BSD  sockets  implementation.
157       Portable  programs  should not rely on inheritance or noninheritance of
158       file status flags and always explicitly set all required flags  on  the
159       socket returned from accept().
160

NOTES

162       POSIX.1-2001  does not require the inclusion of <sys/types.h>, and this
163       header file is not required on Linux.  However, some  historical  (BSD)
164       implementations  required  this  header file, and portable applications
165       are probably wise to include it.
166
167       There may not always be a connection waiting after a SIGIO is delivered
168       or  select(2) or poll(2) return a readability event because the connec‐
169       tion might have been  removed  by  an  asynchronous  network  error  or
170       another  thread  before  accept()  is called.  If this happens then the
171       call will block waiting for the next connection to arrive.   To  ensure
172       that  accept() never blocks, the passed socket sockfd needs to have the
173       O_NONBLOCK flag set (see socket(7)).
174
175   The socklen_t type
176       The third argument of accept() was originally declared as an int * (and
177       is  that  under libc4 and libc5 and on many other systems like 4.x BSD,
178       SunOS 4, SGI); a POSIX.1g draft standard wanted to  change  it  into  a
179       size_t  *, and that is what it is for SunOS 5.  Later POSIX drafts have
180       socklen_t *, and so do the Single Unix Specification and glibc2.  Quot‐
181       ing Linus Torvalds:
182
183       "_Any_  sane  library  _must_ have "socklen_t" be the same size as int.
184       Anything else breaks any BSD socket layer stuff.  POSIX  initially  did
185       make  it  a  size_t, and I (and hopefully others, but obviously not too
186       many) complained to them very loudly indeed.  Making  it  a  size_t  is
187       completely  broken, exactly because size_t very seldom is the same size
188       as "int" on 64-bit architectures, for example.  And it has  to  be  the
189       same  size  as  "int"  because that's what the BSD socket interface is.
190       Anyway,  the  POSIX  people  eventually  got  a   clue,   and   created
191       "socklen_t".   They  shouldn't  have touched it in the first place, but
192       once they did they felt it had to have a named type  for  some  unfath‐
193       omable  reason  (probably  somebody didn't like losing face over having
194       done the original stupid thing, so they  silently  just  renamed  their
195       blunder)."
196

EXAMPLE

198       See bind(2).
199

SEE ALSO

201       bind(2), connect(2), listen(2), select(2), socket(2), socket(7)
202

COLOPHON

204       This  page  is  part of release 3.25 of the Linux man-pages project.  A
205       description of the project, and information about reporting  bugs,  can
206       be found at http://www.kernel.org/doc/man-pages/.
207
208
209
210Linux                             2009-02-23                         ACCEPT(2)
Impressum