[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

Technical Reference: Communications, Volume 2

accept Subroutine


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


Standard C Library (libc.a)


#include <sys/socket.h>

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


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.


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:

EBADF The Socket parameter is not valid.
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.


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);

Implementation Specifics

The accept subroutine is part of Base Operating System (BOS) Runtime.

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.

Related Information

The connect subroutine, getsockname subroutine, listen subroutine, socket subroutine.

Accepting UNIX Stream Connections Example Program, Binding Names to Sockets, Sockets Overview, Understanding Socket Connections, and Understanding Socket Creation in AIX 5L Version 5.1 Communications Programming Concepts.

[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]