IPSocket.accept

Accepts a connection from a listening socket and sets this.fd to the accepted socket file descriptor.

The accept() system call is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET). 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.

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 connec‐ tions 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) or poll(2). 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 SIGIO when activity occurs on a socket; see socket(7) for details.

Parameters

listening_socket ISelectable

the listening socket to accept the new connection from

remote_address InAddr

filled in with the address of the peer socket, as known to the communications layer

flags SocketFlags

The following values can be bitwise ORed in flags:

SOCK_NONBLOCK Set the O_NONBLOCK file status flag on the new open file description. Using this flag saves extra calls to fcntl(2) to achieve the same result.

SOCK_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor. See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful.

Return Value

Type: int

the file descriptor of the accepted socket on success or -1 on failure. On failure errno is set appropriately.

Errors: EAGAIN or EWOULDBLOCK The socket is marked nonblocking and no connections are present to be accepted.

EBADF The descriptor is invalid.

ECONNABORTED A connection has been aborted.

EFAULT The addr argument is not in a writable part of the user address space.

EINTR The system call was interrupted by a signal that was caught before a valid connection arrived; see signal(7).

EINVAL Socket is not listening for connections, or addrlen is invalid (e.g., is negative).

EINVAL invalid value in flags.

EMFILE The per-process limit of open file descriptors has been reached.

ENFILE 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.

ENOTSOCK The descriptor references a file, not a socket.

EOPNOTSUPP The referenced socket is not of type SOCK_STREAM.

EPROTO Protocol error.

In addition, Linux accept() may fail if:

EPERM 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.

Meta