2 accept - accept a connection on a socket
7 int accept(int s, struct sockaddr *addr, int *addrlen);
10 The argument s is a socket that has been created with
11 socket(2), bound to an address with bind(2), and is lis-
12 tening for connections after a listen(2). The accept
13 function extracts the first connection request on the
14 queue of pending connections, creates a new socket with
15 the same properties of s, and allocates a new file
16 descriptor for the socket. If no pending connections are
17 present on the queue, and the socket is not marked as non-
18 blocking, accept blocks the caller until a connection is
19 present. If the socket is marked non-blocking and no
20 pending connections are present on the queue, accept
21 returns an error as described below. The socket returned
22 by accept may not be used to accept more connections. The
23 original socket s remains open.
25 The argument addr is a result parameter that is filled in
26 with the address of the connecting entity, as known to the
27 communications layer. The exact format of the addr param-
28 eter is determined by the domain in which the communica-
29 tion is occurring. addrlen is a value-result parameter:
30 it should initially contain the amount of space pointed to
31 by addr; on return it will contain the actual length (in
32 bytes) of the address returned. This call is used with
33 connection-based socket types, currently with SOCK_STREAM.
35 It is possible to select(2) a socket for the purposes of
36 doing an accept by selecting it for read.
38 For certain protocols which require an explicit confirma-
39 tion, such as DECNet, accept can be thought of as merely
40 dequeuing the next connection request and not implying
41 confirmation. Confirmation can be implied by a normal
42 read or write on the new file descriptor, and rejection
43 can be implied by closing the new socket. Currently only
44 DECNet has these semantics on Linux.
47 If you want accept to never block the listening socket
48 needs to have the non blocking flag set. Assuming that
49 there is always a connection waiting after select returned
50 true is not reliable, because the connection might be
51 removed by an asynchronous network error between the
52 select/poll returning and the accept call. The application
53 would hang then if the listen socket is not non blocking.
56 The call returns -1 on error. If it succeeds, it returns
57 a non-negative integer that is a descriptor for the
61 EBADF The descriptor is invalid.
64 The descriptor references a file, not a socket.
67 The referenced socket is not of type SOCK_STREAM.
69 EAGAIN The socket is marked non-blocking and no connec-
70 tions are present to be accepted.
73 Not enough free memory.