accept

Accepts a connection from a remote host.

Library:LibC
Standard:BSD
Service:Networking

Syntax

  #include <sys/socket.h> 
   
  int accept (
     int               s,   
     struct sockaddr  *addr,   
     size_t           *len);
  

Parameters

s

(IN) Specifies a socket file handle that is waiting for a connection.

addr

(OUT) Points to the location for the address that is assigned to an unbound socket. For IPv4, addr points to a sockaddr structure; for IPv6, to a sockaddr_in6 structure. The first two bytes in the structures identify the protocol family, and hence the structure.

len

(IN/OUT) Points to the amount of space pointed to by addr. Returns the actual length (in bytes) of addr.

Return Values

If successful, returns an integer handle for the accepted socket. Otherwise, returns -1 and sets errno to one of the following:

Decimal

Constant

Description

4

EBADF

The socket file handle is invalid.

9

EINVAL

The socket is not in a listening state.

35

EWOULDBLOCK

The socket is marked nonblocking, and no connections are present to be accepted.

38

ENOTSOCK

The socket file handle refers to a file, not a socket.

45

EOPNOTSUPP

The referenced socket is not of type SOCK_STREAM.

Remarks

The accept function is used with connection-based socket types (SOCK_STREAM).

The s parameter must reference a socket that was created with socket, that has been bound to an address with bind, and that has issued a successful call to listen. The accept function then accepts the connection on a socket, and the original socket is returned to a listening state. It extracts the first connection from the queue pending connections, creates a new socket with the same properties of s, and allocates a new file handle for the socket.

If there are no pending connections, accept behaves in the following manner:

  • If the socket is not marked as nonblocking, accept blocks the caller until a connection is present.

  • If the socket is marked nonblocking and there are no pending connections, accept returns an error.

The accepted socket handle cannot be used to accept more connections. The original socket remains open and continues to listen for connections.

See Also