Section: Linux Programmer's Manual (2)
Return to Main Contents
accept - accept a connection on a socket
#include <sys/types.h> /* See NOTES */
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
#define _GNU_SOURCE /* See feature_test_macros(7) */
int accept4(int sockfd, struct sockaddr *addr,
socklen_t *addrlen, int flags);
system call is used with connection-based socket types
It extracts the first connection request on the queue of pending
connections for the listening socket,
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
is unaffected by this call.
is a socket that has been created with
bound to a local address with
and is listening for connections after a
is a pointer to a
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
is determined by the socket's address family (see
and the respective protocol man pages).
is NULL, nothing is filled in; in this case,
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 address.
The returned address is truncated if the buffer provided is too small;
in this case,
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
blocks the caller until a connection is present.
If the socket is marked
nonblocking and no pending connections are present on the queue,
fails with the error
In order to be notified of incoming connections on a socket, you can use
A readable event will be delivered when a new connection is attempted and you
may then call
to get a socket for that connection.
Alternatively, you can set the socket to deliver
when activity occurs on a socket; see
For certain protocols which require an explicit confirmation,
can be thought of as merely dequeuing the next connection request and not
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.
has these semantics on Linux.
is 0, then
is the same as
The following values can be bitwise ORed in
to obtain different behavior:
file status flag on the new open file description.
Using this flag saves extra calls to
to achieve the same result.
Set the close-on-exec
flag on the new file descriptor.
See the description of the
for reasons why this may be useful.
these system calls return a nonnegative integer that is a descriptor
for the accepted socket.
On error, -1 is returned, and
is set appropriately.
passes already-pending network errors on the new socket
as an error code from
This behavior differs from other BSD socket
For reliable operation the application should detect
the network errors defined for the protocol after
In case of TCP/IP these are
- EAGAIN or EWOULDBLOCK
The socket is marked nonblocking and no connections are
present to be accepted.
POSIX.1-2001 allows either error to be returned for this case,
and does not require these constants to have the same value,
so a portable application should check for both possibilities.
The descriptor is invalid.
A connection has been aborted.
argument 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
Socket is not listening for connections, or
is invalid (e.g., is negative).
invalid value in
The per-process limit of open file descriptors has been reached.
The system 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 descriptor references a file, not 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
may be seen during a trace.
system call is available starting with Linux 2.6.28;
support in glibc is available starting with version 2.10.
first appeared in 4.2BSD).
is a nonstandard Linux extension.
On Linux, the new socket returned by
does not inherit file status flags such as
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
is delivered or
return a readability event because the connection might have been
removed by an asynchronous network error or another thread before
If this happens then the call will block waiting for the next
connection to arrive.
To ensure that
never blocks, the passed socket
needs to have the
flag set (see
The socklen_t type
The third argument of
was originally declared as an int * (and is that under libc4 and libc5
and on many other systems like 4.x BSD, SunOS 4, SGI); a POSIX.1g draft
standard wanted to change it into a size_t *, and that is what it is
for SunOS 5.
Later POSIX drafts have socklen_t *,
and so do the Single Unix Specification and glibc2.
Quoting Linus Torvalds:
"_Any_ sane library _must_ have "socklen_t" be the same size
Anything else breaks any BSD socket layer stuff.
POSIX initially did make it a size_t, and I (and hopefully others, but
obviously not too many) complained to them very loudly indeed.
Making it a size_t is completely broken, exactly because size_t very
seldom is the same size as "int" on 64-bit architectures, for example.
has to be the same size as "int" because that's what the BSD socket
Anyway, the POSIX people eventually got a clue, and created "socklen_t".
They shouldn't have touched it in the first place, but once they did
they felt it had to have a named type for some unfathomable reason
(probably somebody didn't like losing face over having done the original
stupid thing, so they silently just renamed their blunder)."
This page is part of release 3.27 of the Linux
A description of the project,
and information about reporting bugs,
can be found at
- RETURN VALUE
- Error Handling
- CONFORMING TO
- The socklen_t type
- SEE ALSO
This document was created by
using the manual pages.