Commit | Line | Data |
---|---|---|
1a944838 | 1 | .\" Copyright (c) 1983, 1990 The Regents of the University of California. |
da268306 | 2 | .\" All rights reserved. |
95f52c01 | 3 | .\" |
1a944838 | 4 | .\" %sccs.include.redist.man% |
da268306 | 5 | .\" |
1a944838 | 6 | .\" @(#)accept.2 6.5 (Berkeley) %G% |
95f52c01 | 7 | .\" |
f850922e | 8 | .TH ACCEPT 2 "" |
95f52c01 KM |
9 | .UC 5 |
10 | .SH NAME | |
11 | accept \- accept a connection on a socket | |
12 | .SH SYNOPSIS | |
13 | .ft B | |
14 | .nf | |
15 | #include <sys/types.h> | |
16 | #include <sys/socket.h> | |
17 | .PP | |
18 | .ft B | |
19 | ns = accept(s, addr, addrlen) | |
20 | int ns, s; | |
21 | struct sockaddr *addr; | |
22 | int *addrlen; | |
23 | .fi | |
24 | .SH DESCRIPTION | |
25 | The argument | |
26 | .I s | |
a9f72a26 | 27 | is a socket that has been created with |
95f52c01 KM |
28 | .IR socket (2), |
29 | bound to an address with | |
30 | .IR bind (2), | |
31 | and is listening for connections after a | |
32 | .IR listen (2). | |
33 | .I Accept | |
1a944838 | 34 | extracts the first connection request |
95f52c01 KM |
35 | on the queue of pending connections, creates |
36 | a new socket with the same properties of | |
37 | .I s | |
38 | and allocates a new file descriptor, | |
39 | .IR ns , | |
40 | for the socket. If no pending connections are | |
41 | present on the queue, and the socket is not marked | |
42 | as non-blocking, | |
43 | .I accept | |
44 | blocks the caller until a connection is present. | |
45 | If the socket is marked non-blocking and no pending | |
46 | connections are present on the queue, | |
47 | .I accept | |
48 | returns an error as described below. | |
49 | The accepted socket, | |
50 | .IR ns , | |
51 | may not be used | |
52 | to accept more connections. The original socket | |
53 | .I s | |
54 | remains open. | |
55 | .PP | |
56 | The argument | |
57 | .I addr | |
a9f72a26 | 58 | is a result parameter that is filled in with |
95f52c01 KM |
59 | the address of the connecting entity, |
60 | as known to the communications layer. | |
61 | The exact format of the | |
62 | .I addr | |
63 | parameter is determined by the domain in which the communication | |
64 | is occurring. | |
65 | The | |
66 | .I addrlen | |
67 | is a value-result parameter; it should initially contain the | |
68 | amount of space pointed to by | |
69 | .IR addr ; | |
70 | on return it will contain the actual length (in bytes) of the | |
71 | address returned. | |
72 | This call | |
73 | is used with connection-based socket types, currently with SOCK_STREAM. | |
74 | .PP | |
75 | It is possible to | |
76 | .IR select (2) | |
77 | a socket for the purposes of doing an | |
78 | .I accept | |
79 | by selecting it for read. | |
1a944838 KS |
80 | .PP |
81 | For certain protocols which require an explicit confirmation, | |
82 | such as ISO or DATAKIT, | |
83 | one should think of accept as merely dequeueing the next connection | |
84 | request, and not in of itself implying confirmation. | |
85 | Confirmation can be implied by a normal read or write on the new | |
86 | file desciptor, and rejection can be implied by closing the | |
87 | new socket. | |
88 | .PP | |
89 | One can obtain user connection request data without confirming | |
90 | the connection by issuing a | |
91 | recvmsg call with an msg_iovlen of 0 and a non-zero | |
92 | msg_controllen, or by issuing a | |
93 | .IR getsockopt (2) | |
94 | request. | |
95 | Similarly, one can provide user connection rejection information | |
96 | by issuing a sendmsg call with providing only the control information, | |
97 | or by calling | |
98 | .IR setsockopt (2). | |
95f52c01 | 99 | .SH "RETURN VALUE |
bdad3a5a | 100 | The call returns \-1 on error. If it succeeds, it returns a non-negative |
a9f72a26 | 101 | integer that is a descriptor for the accepted socket. |
95f52c01 KM |
102 | .SH ERRORS |
103 | The \fIaccept\fP will fail if: | |
104 | .TP 20 | |
105 | [EBADF] | |
106 | The descriptor is invalid. | |
107 | .TP 20 | |
108 | [ENOTSOCK] | |
109 | The descriptor references a file, not a socket. | |
110 | .TP 20 | |
111 | [EOPNOTSUPP] | |
112 | The referenced socket is not of type SOCK_STREAM. | |
113 | .TP 20 | |
114 | [EFAULT] | |
115 | The \fIaddr\fP parameter is not in a writable part of the | |
116 | user address space. | |
117 | .TP 20 | |
118 | [EWOULDBLOCK] | |
119 | The socket is marked non-blocking and no connections | |
120 | are present to be accepted. | |
121 | .SH SEE ALSO | |
122 | bind(2), connect(2), listen(2), select(2), socket(2) |