1ACCEPT(2) Linux Programmer's Manual ACCEPT(2)
2
3
4
6 accept - accept a connection on a socket
7
9 #include <sys/types.h>
10 #include <sys/socket.h>
11
12 int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
13
15 The accept() system call is used with connection-based socket types
16 (SOCK_STREAM, SOCK_SEQPACKET). It extracts the first connection
17 request on the queue of pending connections, creates a new connected
18 socket, and returns a new file descriptor referring to that socket.
19 The newly created socket is not in the listening state. The original
20 socket sockfd is unaffected by this call.
21
22 The argument sockfd is a socket that has been created with socket(2),
23 bound to a local address with bind(2), and is listening for connections
24 after a listen(2).
25
26 The argument addr is a pointer to a sockaddr structure. This structure
27 is filled in with the address of the peer socket, as known to the com‐
28 munications layer. The exact format of the address returned addr is
29 determined by the socket's address family (see socket(2) and the
30 respective protocol man pages). The addrlen argument is a value-result
31 argument: it should initially contain the size of the structure pointed
32 to by addr; on return it will contain the actual length (in bytes) of
33 the address returned. When addr is NULL nothing is filled in.
34
35 If no pending connections are present on the queue, and the socket is
36 not marked as non-blocking, accept() blocks the caller until a connec‐
37 tion is present. If the socket is marked non-blocking and no pending
38 connections are present on the queue, accept() fails with the error
39 EAGAIN.
40
41 In order to be notified of incoming connections on a socket, you can
42 use select(2) or poll(2). A readable event will be delivered when a
43 new connection is attempted and you may then call accept() to get a
44 socket for that connection. Alternatively, you can set the socket to
45 deliver SIGIO when activity occurs on a socket; see socket(7) for
46 details.
47
48 For certain protocols which require an explicit confirmation, such as
49 DECNet, accept() can be thought of as merely dequeuing the next connec‐
50 tion request and not implying confirmation. Confirmation can be
51 implied by a normal read or write on the new file descriptor, and
52 rejection can be implied by closing the new socket. Currently only DEC‐
53 Net has these semantics on Linux.
54
56 There may not always be a connection waiting after a SIGIO is delivered
57 or select(2) or poll(2) return a readability event because the connec‐
58 tion might have been removed by an asynchronous network error or
59 another thread before accept() is called. If this happens then the
60 call will block waiting for the next connection to arrive. To ensure
61 that accept() never blocks, the passed socket sockfd needs to have the
62 O_NONBLOCK flag set (see socket(7)).
63
65 On success, accept() returns a non-negative integer that is a descrip‐
66 tor for the accepted socket. On error, -1 is returned, and errno is
67 set appropriately.
68
70 Linux accept() passes already-pending network errors on the new socket
71 as an error code from accept(). This behaviour differs from other BSD
72 socket implementations. For reliable operation the application should
73 detect the network errors defined for the protocol after accept() and
74 treat them like EAGAIN by retrying. In case of TCP/IP these are ENET‐
75 DOWN, EPROTO, ENOPROTOOPT, EHOSTDOWN, ENONET, EHOSTUNREACH, EOPNOTSUPP,
76 and ENETUNREACH.
77
79 accept() shall fail if:
80
81 EAGAIN or EWOULDBLOCK
82 The socket is marked non-blocking and no connections are present
83 to be accepted.
84
85 EBADF The descriptor is invalid.
86
87 ECONNABORTED
88 A connection has been aborted.
89
90 EINTR The system call was interrupted by a signal that was caught
91 before a valid connection arrived.
92
93 EINVAL Socket is not listening for connections, or addrlen is invalid
94 (e.g., is negative).
95
96 EMFILE The per-process limit of open file descriptors has been reached.
97
98 ENFILE The system limit on the total number of open files has been
99 reached.
100
101 ENOTSOCK
102 The descriptor references a file, not a socket.
103
104 EOPNOTSUPP
105 The referenced socket is not of type SOCK_STREAM.
106
107 accept() may fail if:
108
109 EFAULT The addr argument is not in a writable part of the user address
110 space.
111
112 ENOBUFS, ENOMEM
113 Not enough free memory. This often means that the memory allo‐
114 cation is limited by the socket buffer limits, not by the system
115 memory.
116
117 EPROTO Protocol error.
118
119 Linux accept() may fail if:
120
121 EPERM Firewall rules forbid connection.
122
123 In addition, network errors for the new socket and as defined for the
124 protocol may be returned. Various Linux kernels can return other errors
125 such as ENOSR, ESOCKTNOSUPPORT, EPROTONOSUPPORT, ETIMEDOUT. The value
126 ERESTARTSYS may be seen during a trace.
127
129 SVr4, 4.4BSD, (accept() first appeared in 4.2BSD), POSIX.1-2001.
130
131 On Linux, the new socket returned by accept() does not inherit file
132 status flags such as O_NONBLOCK and O_ASYNC from the listening socket.
133 This behaviour differs from the canonical BSD sockets implementation.
134 Portable programs should not rely on inheritance or non-inheritance of
135 file status flags and always explicitly set all required flags on the
136 socket returned from accept().
137
139 The third argument of accept() was originally declared as an `int *'
140 (and is that under libc4 and libc5 and on many other systems like 4.x
141 BSD, SunOS 4, SGI); a POSIX.1g draft standard wanted to change it into
142 a `size_t *', and that is what it is for SunOS 5. Later POSIX drafts
143 have `socklen_t *', and so do the Single Unix Specification and glibc2.
144 Quoting Linus Torvalds:
145
146 "_Any_ sane library _must_ have "socklen_t" be the same size as int.
147 Anything else breaks any BSD socket layer stuff. POSIX initially did
148 make it a size_t, and I (and hopefully others, but obviously not too
149 many) complained to them very loudly indeed. Making it a size_t is
150 completely broken, exactly because size_t very seldom is the same size
151 as "int" on 64-bit architectures, for example. And it has to be the
152 same size as "int" because that's what the BSD socket interface is.
153 Anyway, the POSIX people eventually got a clue, and created
154 "socklen_t". They shouldn't have touched it in the first place, but
155 once they did they felt it had to have a named type for some unfath‐
156 omable reason (probably somebody didn't like losing face over having
157 done the original stupid thing, so they silently just renamed their
158 blunder)."
159
160
162 bind(2), connect(2), listen(2), select(2), socket(2)
163
164
165
166Linux 2.6.7 2004-06-17 ACCEPT(2)