ISocket.accept

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

Note: This generic wrapper should be used only in special situations, the subclass variants for a particular address family are preferred.

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.

  1. int accept(ISelectable listening_socket, sockaddr* remote_address, socklen_t addrlen, SocketFlags flags)
    class ISocket
    int
    accept
    (
    ISelectable listening_socket
    ,
    sockaddr* remote_address
    ,
    out socklen_t addrlen
    ,)
  2. int accept(ISelectable listening_socket, SocketFlags flags)
  3. int accept(ISelectable listening_socket, bool nonblocking)

Parameters

listening_socket ISelectable

the file descriptor of the listening socket to accept the new connection from

remote_address sockaddr*

filled in with the address of the peer socket, as known to the communications layer; expected to point to a sin_addr or sin6_addr instance, depending on the IP version of this instance

addrlen socklen_t

actual address struct length output, initialised to this.in_addrlen; returning a different value indicates socket family mixup

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