accept Subroutine

Purpose

Accepts a connection on a socket to create a new socket.

Library

Standard C Library (libc.a)

Syntax

#include <sys/socket.h>

int accept ( Socket,  Address,  AddressLength)
int Socket;
struct sockaddr *Address;
socklen_t *AddressLength;

Description

The accept subroutine extracts the first connection on the queue of pending connections, creates a new socket with the same properties as the specified socket, and allocates a new file descriptor for that socket.

If the listen queue is empty of connection requests, the accept subroutine:

The accepted socket cannot accept more connections. The original socket remains open and can accept more connections.

The accept subroutine is used with SOCK_STREAM and SOCK_CONN_DGRAM socket types.

For SOCK_CONN_DGRAM socket type and ATM protocol, a socket is not ready to transmit/receive data until SO_ATM_ACCEPT socket option is called. This allows notification of an incoming connection to the application, followed by modification of appropriate parameters and then indicate that a connection can become fully operational.

The socket applications can be compiled with COMPAT_43 defined. This will make the sockaddr structure BSD 4.3 compatible. For more details refer to socket.h.

Parameters

Item Description
Socket Specifies a socket created with the socket subroutine that is bound to an address with the bind subroutine and has issued a successful call to the listen subroutine.
Address Specifies 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 occurs.
AddressLength Specifies a parameter that initially contains the amount of space pointed to by the Address parameter. Upon return, the parameter contains the actual length (in bytes) of the address returned. The accept subroutine is used with SOCK_STREAM socket types.

Return Values

Upon successful completion, the accept subroutine returns the nonnegative socket descriptor of the accepted socket.

If the accept subroutine is unsuccessful, the subroutine handler performs the following functions:

Error Codes

The accept subroutine is unsuccessful if one or more of the following is true:

Item Description
EBADF The Socket parameter is not valid.
ECONNRESET The connection has been reset by the partner.
EINTR The accept function was interrupted by a signal that was caught before a valid connection arrived.
EINVAL The socket referenced by s is not currently a listen socket or has been shutdown with shutdown. A listen must be done before an accept is allowed.
EMFILE The system limit for open file descriptors per process has already been reached (OPEN_MAX).
ENFILE The maximum number of files allowed are currently open.
ENOTSOCK The Socket parameter refers to a file, not a socket.
EOPNOTSUPP The referenced socket is not of type SOCK_STREAM.
EFAULT The Address parameter is not in a writable part of the user address space.
EWOULDBLOCK The socket is marked as nonblocking, and no connections are present to be accepted.
ENETDOWN The network with which the socket is associated is down.
ENOTCONN The socket is not in the connected state.
ECONNABORTED The client aborted the connection.

Examples

As illustrated in this program fragment, once a socket is marked as listening, a server process can accept a connection:

struct sockaddr_in from;
.
.
.
fromlen = sizeof(from);
newsock = accept(socket, (struct sockaddr*)&from, &fromlen);