Section (2) accept
accept, accept4 — accept a connection on a socket
#include <sys/types.h> /* See NOTES */ #include <sys/socket.h>
|struct sockaddr *addr,|
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <sys/socket.h>
|struct sockaddr *addr,|
accept() system call is
used with connection-based socket types (
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
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_zsingle_quotesz_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.
argument is a value-result argument: the caller must
initialize it to contain the size (in bytes) of the structure
pointed to by
on return it will contain the actual size of the peer
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
activity occurs on a socket; see socket(7) for details.
flags is 0,
accept4() is the same as
accept(). The following values
can be bitwise ORed in
flags to obtain different
Set the close-on-exec (
FD_CLOEXEC) flag on the new file descriptor. See the description of the
O_CLOEXECflag in open(2) for reasons why this may be useful.
On success, these system calls return a nonnegative
integer that is a file descriptor for the accepted socket. On
error, −1 is returned,
errno is set appropriately, and
addrlen is left unchanged.
already-pending network errors on the new socket as an
error code from
This behavior differs from other BSD socket
implementations. For reliable operation the application
should detect the network errors defined for the protocol
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.
- EAGAIN or EWOULDBLOCK
The socket is marked nonblocking and no connections are present to be accepted. POSIX.1-2001 and POSIX.1-2008 allow either error to be returned for this case, and do not require these constants to have the same value, so a portable application should check for both possibilities.
sockfdis not an open file descriptor.
A connection has been aborted.
addrargument is not in a writable part of the user address space.
The system call was interrupted by a signal that was caught before a valid connection arrived; see signal(7).
Socket is not listening for connections, or
addrlenis invalid (e.g., is negative).
accept4()) invalid value in
The per-process limit on the number of open file descriptors has been reached.
The system-wide limit on the total number of open files has been reached.
- ENOBUFS, ENOMEM
Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory.
The file descriptor
sockfddoes not refer to a socket.
The referenced socket is not of type
In addition, Linux
may fail if:
Firewall rules forbid connection.
In addition, network errors for the new socket and as
defined for the protocol may be returned. Various Linux
kernels can return other errors such as ENOSR, ESOCKTNOSUPPORT, EPROTONOSUPPORT, ETIMEDOUT. The value
ERESTARTSYS may be seen during a trace.
accept4() system call is
available starting with Linux 2.6.28; support in glibc is
available starting with version 2.10.
POSIX.1-2008, SVr4, 4.4BSD (
accept() first appeared in 4.2BSD).
accept4() is a nonstandard
On Linux, the new socket returned by
not inherit file status flags
O_ASYNC from the listening
socket. This behavior differs from the canonical BSD sockets
implementation. Portable programs should not rely on
inheritance or noninheritance of file status flags and always
explicitly set all required flags on the socket returned from
POSIX.1-2001 does not require the inclusion of
> and this header file is not required on
Linux. However, some historical (BSD) implementations
required this header file, and portable applications are
probably wise to include it.
There may not always be a connection waiting after a
SIGIO is delivered or select(2), poll(2), or epoll(7) return a
readability event because the connection might have been
removed by an asynchronous network error or another thread
accept() is called. If
this happens, then the call will block waiting for the next
connection to arrive. To ensure that
accept() never blocks, the passed socket
sockfd needs to have
O_NONBLOCK flag set (see
For certain protocols which require an explicit
confirmation, such as DECnet,
accept() can be thought of as merely
dequeuing the next connection request and not implying
confirmation. Confirmation can be implied by a normal read or
write on the new file descriptor, and rejection can be
implied by closing the new socket. Currently, only DECnet has
these semantics on Linux.
This page is part of release 5.04 of the Linux
man-pages project. A
description of the project, information about reporting bugs,
and the latest version of this page, can be found at
Copyright (c) 1983, 1990, 1991 The Regents of the University of California.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by the University of
California, Berkeley and its contributors.
4. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS_zsingle_quotesz__zsingle_quotesz_ AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Modified 1993-07-24 by Rik Faith <faithcs.unc.edu>
Modified 1996-10-21 by Eric S. Raymond <esrthyrsus.com>
Modified 1998-2000 by Andi Kleen to match Linux 2.2 reality
Modified 2002-04-23 by Roger Luethi <rlhellgate.ch>
Modified 2004-06-17 by Michael Kerrisk <mtk.manpagesgmail.com>
2008-12-04, mtk, Add documentation of accept4()