accept, accept4 - accept a connection on a socket
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *_Nullable restrict addr,
socklen_t *_Nullable restrict addrlen);
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sys/socket.h>
int accept4(int sockfd, struct sockaddr *_Nullable restrict addr,
socklen_t *_Nullable restrict addrlen, int flags);
The accept() system call is used with
connection-based socket types (SOCK_STREAM,
SOCK_SEQPACKET). It extracts the first connection
request on the queue of pending connections for the listening socket,
sockfd
, creates a new connected socket, and returns a new file
descriptor referring to that socket. The newly created socket is not in
the listening state. The original socket sockfd
is unaffected
by this call.
The argument sockfd
is a socket that has been created with
socket(2), bound to a local address with
bind(2), and is listening for connections after a
listen(2).
The argument addr
is a pointer to a sockaddr
structure. This structure is filled in with the address of the peer
socket, as known to the communications layer. The exact format of the
address returned addr
is determined by the socket's address
family (see socket(2) and the respective protocol man
pages). When addr
is NULL, nothing is filled in; in this case,
addrlen
is not used, and should also be NULL.
The addrlen
argument is a value-result argument: the caller
must initialize it to contain the size (in bytes) of the structure
pointed to by addr
; on return it will contain the actual size
of the peer address.
The returned address is truncated if the buffer provided is too
small; in this case, addrlen
will return a value greater than
was supplied to the call.
If no pending connections are present on the queue, and the socket is not marked as nonblocking, accept() blocks the caller until a connection is present. If the socket is marked nonblocking and no pending connections are present on the queue, accept() fails with the error EAGAIN or EWOULDBLOCK.
In order to be notified of incoming connections on a socket, you can use select(2), poll(2), or epoll(7). A readable event will be delivered when a new connection is attempted and you may then call accept() to get a socket for that connection. Alternatively, you can set the socket to deliver SIGIO when activity occurs on a socket; see socket(7) for details.
If flags
is 0, then accept4() is the same
as accept(). The following values can be bitwise ORed
in flags
to obtain different behavior:
Set the O_NONBLOCK file status flag on the open file description (see open(2)) referred to by the new file descriptor. Using this flag saves extra calls to fcntl(2) to achieve the same result.
Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor. See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful.
On success, these system calls return a file descriptor for the
accepted socket (a nonnegative integer). On error, -1 is returned,
errno
is set to indicate the error, and addrlen
is
left unchanged.
Linux accept() (and accept4()) passes already-pending network errors on the new socket as an error code from accept(). This behavior differs from other BSD socket implementations. For reliable operation the application should detect the network errors defined for the protocol after accept() and treat them like EAGAIN by retrying. In the case of TCP/IP, these are ENETDOWN, EPROTO, ENOPROTOOPT, EHOSTDOWN, ENONET, EHOSTUNREACH, EOPNOTSUPP, and ENETUNREACH.
See bind(2).