BSD 4_3_Tahoe release
[unix-history] / usr / src / man / man2 / accept.2
CommitLineData
95f52c01
KM
1.\" Copyright (c) 1983 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
95f51977 5.\" @(#)accept.2 6.3 (Berkeley) 5/22/86
95f52c01 6.\"
95f51977 7.TH ACCEPT 2 "May 22, 1986"
95f52c01
KM
8.UC 5
9.SH NAME
10accept \- accept a connection on a socket
11.SH SYNOPSIS
12.ft B
13.nf
14#include <sys/types.h>
15#include <sys/socket.h>
16.PP
17.ft B
18ns = accept(s, addr, addrlen)
19int ns, s;
20struct sockaddr *addr;
21int *addrlen;
22.fi
23.SH DESCRIPTION
24The argument
25.I s
a9f72a26 26is a socket that has been created with
95f52c01
KM
27.IR socket (2),
28bound to an address with
29.IR bind (2),
30and is listening for connections after a
31.IR listen (2).
32.I Accept
33extracts the first connection
34on the queue of pending connections, creates
35a new socket with the same properties of
36.I s
37and allocates a new file descriptor,
38.IR ns ,
39for the socket. If no pending connections are
40present on the queue, and the socket is not marked
41as non-blocking,
42.I accept
43blocks the caller until a connection is present.
44If the socket is marked non-blocking and no pending
45connections are present on the queue,
46.I accept
47returns an error as described below.
48The accepted socket,
49.IR ns ,
50may not be used
51to accept more connections. The original socket
52.I s
53remains open.
54.PP
55The argument
56.I addr
a9f72a26 57is a result parameter that is filled in with
95f52c01
KM
58the address of the connecting entity,
59as known to the communications layer.
60The exact format of the
61.I addr
62parameter is determined by the domain in which the communication
63is occurring.
64The
65.I addrlen
66is a value-result parameter; it should initially contain the
67amount of space pointed to by
68.IR addr ;
69on return it will contain the actual length (in bytes) of the
70address returned.
71This call
72is used with connection-based socket types, currently with SOCK_STREAM.
73.PP
74It is possible to
75.IR select (2)
76a socket for the purposes of doing an
77.I accept
78by selecting it for read.
79.SH "RETURN VALUE
bdad3a5a 80The call returns \-1 on error. If it succeeds, it returns a non-negative
a9f72a26 81integer that is a descriptor for the accepted socket.
95f52c01
KM
82.SH ERRORS
83The \fIaccept\fP will fail if:
84.TP 20
85[EBADF]
86The descriptor is invalid.
87.TP 20
88[ENOTSOCK]
89The descriptor references a file, not a socket.
90.TP 20
91[EOPNOTSUPP]
92The referenced socket is not of type SOCK_STREAM.
93.TP 20
94[EFAULT]
95The \fIaddr\fP parameter is not in a writable part of the
96user address space.
97.TP 20
98[EWOULDBLOCK]
99The socket is marked non-blocking and no connections
100are present to be accepted.
101.SH SEE ALSO
102bind(2), connect(2), listen(2), select(2), socket(2)