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