accept -- accept a connection on a socket


cc [options] file -lsocket -lnsl
#include <sys/types.h>
#include <sys/socket.h>

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


The argument s is a socket that has been created with socket and bound to an address with bind, and that is listening for connections after a call to listen. accept extracts the first connection on the queue of pending connections, creates a new socket with the properties of s, and allocates a new file descriptor, ns, for the socket. If no pending connections are present on the queue and the socket is not marked as non-blocking (O_NONBLOCK), accept blocks the caller until a connection is present. If the socket is marked as non-blocking and no pending connections are present on the queue, accept returns an error as described below. accept uses the netconfig file to determine the STREAMS device file name associated with s. This is the device on which the connect indication will be accepted. The accepted socket, ns, is used to read and write data to and from the socket that connected to ns; it is not used to accept more connections. The original socket (s) remains open for accepting further connections.

The argument addr is a result parameter that is filled in with the address of the connecting entity as it is known to the communications layer. The exact format of the addr parameter is determined by the domain in which the communication occurs.

addrlen is a value-result parameter. Initially, it contains the amount of space pointed to by addr; on return it contains the length in bytes of the address returned.

accept is used with connection-based socket types, currently with SOCK_STREAM.

It is possible to select a socket for the purpose of an accept by selecting it for read. However, this will only indicate when a connect indication is pending; it is still necessary to call accept.



Return values

accept returns -1 on error. If it succeeds, it returns a non-negative integer that is a descriptor for the accepted socket.


accept will fail if:

The descriptor is invalid.

The connection was aborted.

The connection was interrupted.

An invalid argument was passed.

Ran out of file descriptors.

Ran out of file table entries.

Ran out of memory for STREAMS or socket buffers.

The protocol family and type corresponding to s could not be found in the netconfig file.

There was insufficient user memory available to complete the operation.

There were insufficient STREAMS resources available to complete the operation.

The descriptor does not reference a socket.

The referenced socket is not of type SOCK_STREAM.

A protocol error has occurred; for example, the STREAMS protocol stack has not been initialized.

The socket is marked as non-blocking and no connections are present to be accepted.


bind(3sock), connect(3sock), listen(3sock), netconfig(4bnu), socket(3sock)

RFC 2133


The type of address structure passed to accept depends on the address family. UNIX® domain sockets (address family AF_UNIX) require a sockaddr_un structure as defined in sys/un.h; Internet domain IPv4 sockets (address family AF_INET) require a sockaddr_in structure as defined in netinet/in.h; Internet domain IPv6 sockets (address family AF_INET6) require a sockaddr_in6 structure as defined in netinet/in.h. Other address families may require other structures. Use the structure appropriate to the address family; cast the structure address to a generic struct sockaddr * in the call to accept and pass the size of the structure in the addrlen argument.

In UnixWare 7 the sockaddr structure has been modified to support variable length sockets. The net result of this modification is that the family member has been shortened to 8 bits and a new 8-bit member inserted before it called len. For more information on the new sockaddr structures, see: unix(7sock) and inet(7tcp)

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004