'\"macro stdmacro
.if n .pH g3n.accept @(#)accept	40.7 of 1/29/90
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} accept 3N "" "\&"
.if \nX=1 .ds x} accept 3N ""
.if \nX=2 .ds x} accept 3N "" "\&"
.if \nX=3 .ds x} accept "" "" "\&"
.TH \*(x}
.SH NAME
\f4accept\fP \- accept a connection on a socket
.SH SYNOPSIS
.ft 4
.nf
#include <sys/types.h>
#include <sys/socket.h>
.LP
.ft 4
ns = accept(s, addr, addrlen)
int ns, s;
struct sockaddr *addr;
int *addrlen;
.fi
.ft R
.SH DESCRIPTION
.LP
The argument
.I s
is a socket that has been created with
\f4socket\fP(3N) and
bound to an address with
\f4bind\fP(3N),
and that is listening for connections after a call to
\f4listen\fP(3N).
\f4accept\fP(\|)
extracts the first connection
on the queue of pending connections, creates
a new socket with the properties of
\f2s\f1,
and allocates a new file descriptor,
.IR ns ,
for the socket.
If no pending connections are
present on the queue and the socket is not marked
as non-blocking,
\f4accept\fP(\|)
blocks the caller until a connection is present.
If the socket is marked as non-blocking and no pending
connections are present on the queue,
\f4accept\fP(\|)
returns an error as described below.
\f4accept\fP(\|)
uses the
\f4netconfig\fP(4)
file to determine the
.SM STREAMS
device file name associated with
\f2s\f1. 
This is the device on which the connect indication will be accepted.
The accepted socket,
.IR ns ,
is used to read and write data to and from the socket that connected
to \f2ns\f1; it is not used
to accept more connections.
The original socket
(\f2s\f1)
remains open for accepting further connections.
.LP
The argument
.I addr
is a result parameter that is filled in with
the address of the connecting entity
as it is known to the communications layer.
The exact format of the
.I addr
parameter is determined by the domain in which the communication
occurs.
.P
.I addrlen
is a value-result parameter.
Initially, it contains the
amount of space pointed to by
.IR addr ;
on return it contains the length in bytes of the
address returned.
.P
\f4accept\fP(\|)
is used with connection-based socket types, currently with
\f4SOCK_STREAM\fP.
.LP
It is possible to
\f4select\fP(3N)
a socket for the purpose of 
an \f4accept\fP(\|)
by selecting it for read.
However, this will only indicate when a
connect indication is pending; it is still necessary to call
\f4accept\fP(\|).
.SH RETURN VALUE
\f4accept\fP(\|) returns \-1 on error.
If it succeeds, it returns a non-negative
integer that is a descriptor for the accepted socket.
.SH ERRORS
\f4accept\fP(\|)
will fail if:
.TP 20
\f4EBADF\f1
The descriptor is invalid.
.TP
\f4ENOTSOCK\f1
The descriptor does not reference a socket.
.TP
\f4EOPNOTSUPP\f1
The referenced socket is not of type
\f4SOCK_STREAM\fP.
.TP
\f4EWOULDBLOCK\f1
The socket is marked as non-blocking and no connections
are present to be accepted.
.TP
\f4EPROTO\f1
A protocol error has occurred;
for example, the
.SM STREAMS
protocol stack has not been initialized.
.TP
\f4ENODEV\f1
The protocol family and type corresponding to 
.I s
could not be found in the
\f4netconfig\f1 file.
.TP
\f4ENOMEM\f1
There was insufficient user memory available to complete the
operation.
.TP
\f4ENOSR\f1
There were insufficient
.SM STREAMS
resources available to complete
the operation.
.SH SEE ALSO
\f4bind\fP(3N),
\f4connect\fP(3N),
\f4listen\fP(3N),
\f4socket\fP(3N),
\f4netconfig\fP(4).
