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 on a socket

&pagelevel(3)&pagelevel

#include <sys.types.h>
#include <sys.socket.h>
 

#include <netinet.in.h> /* only for AF_INET and AF_INET 6 */
#include <iso.h> /* only for AF_ISO */
   
Kernighan-Ritchie-C:
int accept(s, addr, addrlen);

int s;
int *addrlen;

struct sockaddr_in *addr; /* only for AF_INET */
struct sockaddr_in6 *addr; /* only for AF_INET6 */
struct sockaddr_iso *addr; /* only for AF_ISO */

ANSI-C:
int accept(int s, struct sockaddr * addr, int* addrlen);


Description

The accept() function is used by the server task to accept a connection on socket s, as requested by the client with the connect() function.

In order to call accept() for socket s, the following requirements must be satisfied:

  • s must be s stream socket (SOCK_STREAM) that has assigned a name (address) with bind().

  • s must be marked with listen(), i.e. identified as a socket on which connection requests can be accepted.

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

addrlen points to an integer object that holds the size of the memory area referenced by *addr (in bytes) at the time of the accept() call. When the accept() function returns, *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 from the connection requests in the queue.

  2. accept() creates a new socket.

  3. accept() returns the descriptor of the new socket as its result.

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

  • If the socket is marked as blocking (standard case), accept() blocks the calling task until a connection is possible.

  • If the socket is marked as non-blocking, accept() returns an error message with errno = EWOULDBLOCK.

You can call select() before calling accept() to test the read readiness of the socket concerned and make sure that the accept() call will not block.

Once accept() has executed successfully, the complete connection will have been set up in the AF_INET and AF_INET6 address families. In the AF_ISO address family, one of the two following steps is also required to set up a complete connection (see also figure 4 in section "Interaction between functions for connection-oriented communications"):

Once a connection has been set up successfully, data can be exchanged via the new socket created by accept() with the socket that requested the connection. Additional connections cannot be set up on the new socket. The original socket s remains open to accept further connections.

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 indicated by errno

EBADF

s is not a valid descriptor.

EFAULT

The length of the range for accepting the address is too small.

EMFILE

The maximum number of open sockets has been reached.

ENETDOWN

The connection to the network is down.

EOPNOTSUPP

The referenced socket is not of type SOCK_STREAM or was not marked with listen() as a socket that can accept connection requests.

EWOULDBLOCK

The socket is marked as non-blocking, and no free connections are available.

See also

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