This document was created by man2html using the manual pages.
Section: Linux Programmer's Manual (2)
Return to NetAdminTools
accept - accept a connection on a socket
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
function is used with connection-based socket types
It extracts the first connection request on the queue of pending
connections, creates a new connected socket with mostly the same properties as
and allocates a new file descriptor for the socket, which is returned.
The newly created socket is no longer in the listening state.
The original socket
is unaffected by this call. Note that any per file descriptor flags
(everything that can be set with the
fcntl, like non blocking or async state) are not inherited across
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 sockaddr structure. This structure is filled in
with the address of the connecting entity,
as known to the communications layer. The exact format of the
address passed in the
parameter is determined by the socket's family (see
and the respective protocol man pages).
argument is a value-result parameter: it should initially contain the
size of the structure pointed to by
on return it will contain the actual length (in bytes) of the address
is NULL nothing is filled in.
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
non-blocking and no pending connections are present on the queue,
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
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
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
has these semantics on Linux.
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 call returns -1 on error. If it succeeds, it returns a non-negative
integer that is a descriptor for the accepted socket.
passes already-pending network errors on the new socket
as an error code from
This behaviour differs from other BSD socket
implementations. For reliable operation the application should detect
the network errors defined for the protocol after
by retrying. In case of TCP/IP these are
shall fail if:
- EAGAIN or EWOULDBLOCK
The socket is marked non-blocking and no connections are
present to be accepted.
The descriptor is invalid.
The descriptor references a file, not a socket.
The referenced socket is not of type
The system call was interrupted by a signal that was caught
before a valid connection arrived.
A connection has been aborted.
Socket is not listening for connections.
The per-process limit of open file descriptors has been reached.
The system maximum for file descriptors has been reached.
may fail if:
parameter is not in a writable part of the user address space.
- 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.
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.
SVr4, 4.4BSD (the
function first appeared in BSD 4.2).
The BSD man page documents five possible error returns
(EBADF, ENOTSOCK, EOPNOTSUPP, EWOULDBLOCK, EFAULT).
SUSv3 documents errors EAGAIN, EBADF, ECONNABORTED, EINTR, EINVAL, EMFILE,
ENFILE, ENOBUFS, ENOMEM, ENOTSOCK, EOPNOTSUPP, EPROTO, EWOULDBLOCK. In
addition, SUSv2 documents EFAULT and ENOSR.
Linux accept does _not_ inherit socket flags like
This behaviour differs from other BSD socket implementations.
Portable programs should not rely on this behaviour and always set
all required flags on the socket returned from accept.
The third argument of
was originally declared as an `int *' (and is that under libc4 and libc5
and on many other systems like BSD 4.*, SunOS 4, SGI); a POSIX 1003.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
Quoting Linus Torvalds:
_Any_ sane library _must_ have "socklen_t" be the same size
as int. 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. And it
_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).
- RETURN VALUE
- ERROR HANDLING
- CONFORMING TO
- SEE ALSO
Return to NetAdminTools,