accept()

accept a connection on a socket 

Function


SYNOPSIS

#include <sys/types.h>

#include <sys/socket.h>

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


DESCRIPTION

The accept() function accepts a connection on a socket. An incoming connection is acknowledged and associated with an immediately created socket. The original socket is returned to the listening state.

The parameter 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 same properties of s, and returns a new handle to the socket. If no pending connections are present on the queue and the socket is not marked as non-blocking, 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. The accepted socket is used to read and write data to and from the socket that connected to it. However, it is not used to accept more connections. The original socket remains open to accept further connections.

The accept() function is used with connection-based socket types (such as SOCK_STREAM).

You can select a socket for the purpose of an accept() by selecting it for read. However, this only indicates when a connect indication is pending; it is still necessary to call accept().


PARAMETERS

s 

Specifies the descriptor of the socket that is waiting for a connection after a listen.

addr 

This is a result parameter that is filled in with the address of the connecting entity (as known to the communications layer). The domain in which the communication is occurring determines the exact format of addr. If addr is NULL, no information about the remote address of the accepted socket is returned.

addrlen 

This is a value-result parameter. Initially, addrlen contains the amount of space that addr points to. When returned, it contains the actual length (in bytes) of the address returned. If addrlen is NULL, no information about the remote address of the accepted socket is returned.


RETURN VALUES

If successful, accept() returns a non-negative integer, which is a descriptor of the accepted socket. Upon return, addrlen contains the actual length in bytes of the address returned. On failure, it returns a value of -1 and sets errno to one of the following values:

EBADF 

s is not a valid descriptor.

EFAULT 

addr or addrlen is an invalid pointer, or addrlen is too small (less than the size of a struct sockaddr).

EINTR 

A signal interrupted the call.

EINVAL 

listen() was not invoked prior to accept().

EMFILE 

Cannot open a file: OPEN_MAX file descriptors are currently open in the calling process.

ENETDOWN 

The network subsystem has failed.

ENOBUFS 

No buffer space is available.

ENOTSOCK 

The descriptor is not a socket.

EOPNOTSUPP 

The referenced socket is not a type that supports connection-oriented service.

EWOULDBLOCK 

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


CONFORMANCE

UNIX 98, with exceptions.


MULTITHREAD SAFETY LEVEL

MT-Safe.


PORTING ISSUES

The NuTCRACKER Platform supports all the address families that WinSock 2.0 supports.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
bind(), connect(), listen(), select(), socket()


PTC MKS Toolkit 10.2 Documentation Build 28.