---
name: "accept"
section: "2"
platform: "macos"
package: "xnu"
version: "12377.121.6"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/accept/2"
---

# accept(2)

accept a connection on a socket

## NAME

**accept** – accept a connection on a socket

## SYNOPSIS

**#include \<sys/socket.h\>**

*int*\

**accept**(*int socket*, *struct sockaddr \*restrict address*, *socklen\_t \*restrict address\_len*);

## DESCRIPTION

The argument *socket* is a socket that has been created with [socket(2)](/socket/2.md), bound to an address with [bind(2)](/bind/2.md), and is listening for connections after a [listen(2)](/listen/2.md). **accept**() extracts the first connection request on the queue of pending connections, creates a new socket with the same properties of  *socket*, and allocates a new file descriptor for 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 non-blocking and no pending connections are present on the queue,  **accept**() returns an error as described below. The accepted socket may not be used to accept more connections.  The original socket *socket,* remains open.

The argument *address* is a result parameter that is filled in with the address of the connecting entity, as known to the communications layer. The exact format of the *address* parameter is determined by the domain in which the communication is occurring. The  *address\_len* is a value-result parameter; it should initially contain the amount of space pointed to by *address*; on return it will contain the actual length (in bytes) of the address returned. This call is used with connection-based socket types, currently with SOCK\_STREAM.

It is possible to [select(2)](/select/2.md) a socket for the purposes of doing an **accept**() by selecting it for read.

For certain protocols which require an explicit confirmation, such as ISO or DATAKIT, **accept**() 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.

One can obtain user connection request data without confirming the connection by issuing a  [recvmsg(2)](/recvmsg/2.md) call with an *msg\_iovlen* of 0 and a non-zero *msg\_controllen*, or by issuing a [getsockopt(2)](/getsockopt/2.md) request. Similarly, one can provide user connection rejection information by issuing a [sendmsg(2)](/sendmsg/2.md) call with providing only the control information, or by calling [setsockopt(2)](/setsockopt/2.md).

## RETURN VALUES

The call returns -1 on error and the global variable *errno* is set to indicate the error. If it succeeds, it returns a non-negative integer that is a descriptor for the accepted socket.

## ERRORS

The **accept**() system call will fail if:

- \[EBADF\]

  *socket* is not a valid file descriptor.

- \[ECONNABORTED\]

  The connection to *socket* has been aborted.

- \[EFAULT\]

  The *address* parameter is not in a writable part of the user address space.

- \[EINTR\]

  The **accept**() system call was terminated by a signal.

- \[EINVAL\]

  *socket* is unwilling to accept connections.

- \[EMFILE\]

  The per-process descriptor table is full.

- \[ENFILE\]

  The system file table is full.

- \[ENOMEM\]

  Insufficient memory was available to complete the operation.

- \[ENOTSOCK\]

  *socket* references a file type other than a socket.

- \[EOPNOTSUPP\]

  *socket* is not of type SOCK\_STREAM and thus does not accept connections.

- \[EWOULDBLOCK\]

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

## LEGACY SYNOPSIS

**#include \<sys/types.h\>** **#include \<sys/socket.h\>**

The include file ⟨sys/types.h⟩ is necessary.

## SEE ALSO

[bind(2)](/bind/2.md), [connect(2)](/connect/2.md), [connectx(2)](/connectx/2.md), [listen(2)](/listen/2.md), [select(2)](/select/2.md), [socket(2)](/socket/2.md), [compat(5)](/compat/5.md)

## HISTORY

The **accept**() function appeared in  4.2BSD.
