Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

accept() - accept a connection over a socket

&pagelevel(4)&pagelevel

#include <sys/socket.h>

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

Description

The server process accepts a connection, which was requested by the client with the connect() function, over a socket with the accept() function. accept() can only be used with the connection-oriented socket type SOCK_STREAM.

The s parameter designates the socket which waits for a connection request after listen() is called.

After returning from accept(), addr points to the address of the partner application as it is known on the communications level. The exact format of *addr (i.e. the address) is determined by the domain in which communication takes place.

The section "Socket addressing" describes how you assign an address to the socket.

addrlen points to a size_t object that holds the size of the memory area referenced by addr at the time of the accept() call. When the accept() function returns, the size_t object (i.e. *addrlen) contains the length of the returned address in bytes.

When the queue set up by the listen() function contains at least one connection request, accept() proceeds as follows:

  1. accept() selects the first connection request in the queue.

  2. accept() creates a new socket with the same properties as socket s.

  3. accept() returns the descriptor of the new socket as its result. If socket s is non-blocking, neither is the new socket (see "fcntl() - control sockets").

Two cases must be considered if there are no connection requests in the queue:

  • If the socket is not marked as non-blocking, accept() blocks the calling process until a connection request arrives.

  • If the socket is marked as non-blocking, accept() returns the error EWOULDBLOCK.

To ensure that the accept() call does not block, the user can first use select() or poll() to check whether the socket in question is ready to read before calling accept().

Once accept() has accepted a connection for socket s, data can be exchanged between the new socket created by accept() and the socket that requested the connection. Additional connections cannot be set up over the new socket. The original socket s remains open to accept further connection requests.

Return value

≥ 0:

If successful. The value is the descriptor for the accepted socket.

-1:

If errors occur. errno is set to indicate the error.

Errors

EBADF

s is not a valid descriptor.

EFAULT

addr does not point to the writable part of the user address range.

EINTR

The accept() function was interrupted by a signal that was received before a connection request was received.

EINVAL

The socket does not accept any connection requests.

EMFILE

OPEN_MAX file descriptors are currently open in the calling process.

ENETDOWN

The connection to the network is down.

ENOBUFS

No buffer space is available.

ENOTSOCK

The descriptor does not refer to a socket.

EOPNOTSUPP

The referenced socket is not of type SOCK_STREAM.

EPROTO

A protocol error occurred.

EWOULDBLOCK

The socket is not marked as non-blocking and has no pending connection requests.

See also

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