accept(3)

NAME
accept - accept a connection on a socket

SYNOPSIS
#include <sys/socket.h>

  AF_UNIX only:
#include <sys/un.h>

int accept(int s, struct sockaddr *addr, size_t *addrlen);

DESCRIPTION
This call is used with connection-based socket types, such as SOCK_STREAM. The argument s is a socket descriptor created with socket(3), bound to an address with bind(3), and listening for connections after a listen(3). accept extracts the first connection on the queue of pending connections, creates a new socket with the same properties as s, and allocates a new file descriptor, ns, for the socket. If no pending connections are present on the queue and non-blocking mode has not been enabled using the O_NDELAY fcntl flag, accept blocks the caller until a connection is present. O_NDELAY is defined in <fcntl.h>. If the socket is marked non-blocking and no pending connections are present on the queue, accept returns an error as described below. The accepted socket, ns, cannot be used to accept more connections. The original socket s remains open. It is possible to determine if a listening socket has pending connection requests ready for an accept call by using select(3) for reading.

The argument addr should point to a local socket address structure. The accept call fills in this structure with the address of the connecting entity, as known to the underlying protocol. The format of the address depends upon the protocol and the address-family of the socket s. The addrlen is a pointer to an argument of type size_t; it should initially contain the size of the structure pointed to by addr. On return, it contains the actual length (in bytes) of the address returned. If the memory pointed to by addr is not large enough to contain the entire address, only the first addrlen bytes of the address are returned.

RETURN VALUE
The call returns -1 on error. If successful, it returns a non-negative integer which is a descriptor for the accepted socket.

DIAGNOSTICS
accept fails if:

[EBADF]
    The file descriptor s is invalid.

[EOPNOTSUPP]
    The socket referenced by s is not of type SOCK_STREAM.

[EWOULDBLOCK]
    Non-blocking I/O is enabled using O_NDELAY and no connections are present to be accepted.

[EMFILE]
    The maximum number of file descriptors for this process are already currently open.

[ENOMEM]
    No buffer space is available. The accept cannot complete. The queued socket connect request is aborted.

[ECONNABORTED]
    The socket referenced by s has been shutdown for reading.

[EINVAL]
    The socket referenced by s is not currently a listening socket or has been fully shutdown. A listen must be done before an accept is allowed. The file descriptor s references a file, not a socket.

[EINTR]
    The call was interrupted by a signal before a valid connection arrived.

SEE ALSO
bind(3), connect(3), listen(3), select(3), socket(3).