Penguin
Annotated edit history of accept(2) version 4, including all changes. View license author blame.
Rev Author # Line
1 perry 1 ACCEPT
2 !!!ACCEPT
3 ----
4 !!NAME
5
6
7 accept - accept a connection on a socket
8 !!SYNOPSIS
9
10
2 PerryLorier 11 #include <sys/types.h>
12 #include <sys/socket.h>
1 perry 13
14
2 PerryLorier 15 __int accept(int__ ''s''__, struct sockaddr *__''addr''__, socklen_t *__''addrlen''__);__
1 perry 16
2 PerryLorier 17 !!DESCRIPTION
1 perry 18
19 The __accept__ function is used with connection-based
20 socket types (__SOCK_STREAM__, __SOCK_SEQPACKET__ and
21 __SOCK_RDM__). It extracts the first connection request
22 on the queue of pending connections, creates a new connected
23 socket with mostly the same properties as ''s'', and
24 allocates a new file descriptor for the socket, which is
25 returned. The newly created socket is no longer in the
26 listening state. The original socket ''s'' is unaffected
27 by this call. Note that any per file descriptor flags
28 (everything that can be set with the __F_SETFL__ fcntl,
29 like non blocking or async state) are not inherited across
30 an ''accept''.
31
32 The argument ''s'' is a socket that has been created with
33 socket(2), bound to a local address with
34 bind(2), and is listening for connections after a
35 listen(2).
36
37 The argument ''addr'' is a pointer to a sockaddr
38 structure. This structure is filled in with the address of
39 the connecting entity, as known to the communications layer.
40 The exact format of the address passed in the ''addr''
41 parameter is determined by the socket's family (see
42 socket(2) and the respective protocol man pages). The
43 ''addrlen'' argument is a value-result parameter: it
44 should initially contain the size of the structure pointed
45 to by ''addr''; on return it will contain the actual
46 length (in bytes) of the address returned. When ''addr''
47 is NULL nothing is filled in.
48
49 If no pending connections are present on the queue, and the
50 socket is not marked as non-blocking, __accept__ blocks
51 the caller until a connection is present. If the socket is
52 marked non-blocking and no pending connections are present
53 on the queue, __accept__ returns EAGAIN.
54
55 In order to be notified of incoming connections on a socket,
56 you can use select(2) or poll(2). A readable
57 event will be delivered when a new connection is attempted
58 and you may then call __accept__ to get a socket for that
59 connection. Alternatively, you can set the socket to deliver
60 __SIGIO__ when activity occurs on a socket; see
61 socket(7) for details.
62
63 For certain protocols which require an explicit
64 confirmation, such as DECNet, __accept__ can be thought
65 of as merely dequeuing the next connection request and not
66 implying confirmation. Confirmation can be implied by a
67 normal read or write on the new file descriptor, and
68 rejection can be implied by closing the new socket.
69 Currently only DECNet has these semantics on
70 Linux.
71
2 PerryLorier 72 !!NOTES
1 perry 73
74 There may not always be a connection waiting after a
75 __SIGIO__ is delivered or select(2) or
76 poll(2) return a readability event because the
77 connection might have been removed by an asynchronous
78 network error or another thread before __accept__ is
79 called. If this happens then the call will block waiting for
80 the next connection to arrive. To ensure that __accept__
81 never blocks, the passed socket ''s'' needs to have the
82 __O_NONBLOCK__ flag set (see
83 socket(7)).
2 PerryLorier 84
1 perry 85 !!RETURN VALUE
86
87 The call returns -1 on error. If it succeeds, it returns a
88 non-negative integer that is a descriptor for the accepted
89 socket.
90
2 PerryLorier 91 !!ERROR HANDLING
1 perry 92
93 Linux __accept__ passes already-pending network errors on
94 the new socket as an error code from __accept__. This
95 behaviour differs from other BSD socket implementations. For
96 reliable operation the application should detect the network
97 errors defined for the protocol after __accept__ and
2 PerryLorier 98 treat them like [EAGAIN] by retrying. In case of TCP/IP
99 these are [ENETDOWN], [EPROTO], [ENOPROTOOPT], [EHOSTDOWN], [ENONET],
100 [EHOSTUNREACH], [EOPNOTSUPP], and [ENETUNREACH].
1 perry 101 !!ERRORS
102
103
2 PerryLorier 104 [EAGAIN] or [EWOULDBLOCK]
1 perry 105
106 The socket is marked non-blocking and no connections are
107 present to be accepted.
108
2 PerryLorier 109 ;[EBADF]: The descriptor is invalid.
110 ;[ENOTSOCK]: The descriptor references a file, not a socket.
111 ;[EOPNOTSUPP]: The referenced socket is not of type __SOCK_STREAM__.
112 ;[EFAULT]: The ''addr'' parameter is not in a writable part of the user address space.
113 ;[EPERM]: Firewall rules forbid connection.
114 ;[ENOBUFS], [ENOMEM]: Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory.
1 perry 115
116 In addition, network errors for the new socket and as
117 defined for the protocol may be returned. Various Linux
2 PerryLorier 118 kernels can return other errors such as [EMFILE],
119 [EINVAL], [ENOSR], [ENOBUFS], [EPERM],
120 [ECONNABORTED], [ESOCKTNOSUPPORT],
4 PerryLorier 121 [EPROTONOSUPPORT], [ETIMEDOUT],[ERESTARTSYS].
1 perry 122 !!CONFORMING TO
123
124 SVr4, 4.4BSD (the __accept__ function first appeared in
125 BSD 4.2). The BSD man page documents five possible error
2 PerryLorier 126 returns ([EBADF], [ENOTSOCK], [EOPNOTSUPP], [EWOULDBLOCK], [EFAULT]).
127 SUSv2 documents errors [EAGAIN], [EBADF], [ECONNABORTED], [EFAULT],
128 [EINTR], [EINVAL], [EMFILE], [ENFILE], [ENOBUFS], [ENOMEM], [ENOSR],
129 [ENOTSOCK], [EOPNOTSUPP], [EPROTO], [EWOULDBLOCK].
1 perry 130
131 Linux accept does _not_ inherit socket flags like
132 __O_NONBLOCK__. This behaviour differs from other BSD
133 socket implementations. Portable programs should not rely on
134 this behaviour and always set all required flags on the
135 socket returned from accept.
2 PerryLorier 136
1 perry 137 !!NOTE
138
2 PerryLorier 139 The third argument of __accept__ was originally declared as an `int *' (and is that under libc4 and libc5 and on many other systems like BSD 4.*, SunOS 4, SGI); a POSIX 1003.1g draft standard wanted to change it into a `size_t *', and that is what it is for SunOS 5. Later POSIX drafts have `socklen_t *', and so do the Single Unix Specification and glibc2. Quoting Linus Torvalds: ''_Any_ sane library _must_ have "socklen_t" be the same size as int. Anything else breaks any BSD socket layer stuff. POSIX initially _did_ make it a size_t, and I (and hopefully others, but obviously not too many) complained to them very loudly indeed. Making it a size_t is completely broken, exactly because size_t very seldom is the same size as "int" on 64-bit architectures, for example. And it _has_ to be the same size as "int" because that's what the BSD socket interface is. Anyway, the POSIX people eventually got a clue, and created "socklen_t". They shouldn't have touched it in the first place, but once they did they felt it had to have a named type for some unfathomable reason (probably somebody didn't like losing face over having done the original stupid thing, so they silently just renamed their blunder).''
1 perry 140 !!SEE ALSO
141
3 PerryLorier 142 bind(2), connect(2), listen(2), select(2), socket(2), CategorySocketSysCalls
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.

PHP Warning

lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 10 times)