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