accept
Purpose
Accepts a connection on a socket.
Library
Sockets Library (libsock.a)
Syntax
#include <sys/types.h>
#include <sys/socket.h>
int accept (s, addr, addrlen)
int s;
struct sockaddr *addr;
int *addrlen;
Description
The accept subroutine extracts the first connection on
the queue of pending connections, creates a new socket
with the same properties as s, and allocates a new file
descriptor for that socket. If no pending connections
are present on the queue and the calling socket is not
marked as non-blocking, accept blocks the caller until a
connection is present. If the socket specified by s is
marked non-blocking and there are no connections pending
on the queue, accept returns an error as described below.
The accepted socket cannot be used to accept more con-
nections. The original socket, s, remains open and can
accept more connections.
The s parameter is a socket that was created with the
socket subroutine, was bound to an address with the bind
subroutine, and has issued a successful call to the
listen subroutine.
The addr parameter is a result parameter that is filled
in with the address of the connecting entity, as known to
the communications layer. The exact format of addr is
determined by the domain in which the communication
occurs. The addrlen parameter initially contains the
amount of space pointed to by the addr parameter. On
return, it contains the actual length (in bytes) of the
address returned. This subroutine is used with
connection-based socket types, such as SOCK_STREAM.
Before calling the accept subroutine, you can find out if
the socket is ready to accept the connection by doing a
read select with the select system call.
Return Value
Upon successful completion, the nonnegative socket
descriptor of the accepted socket is returned. If the
accept routine fails, a value of -1 is returned, and
errno is set to indicate the error.
Diagnostics
The subroutine fails if one or more of the following are
true:
EBADF The s parameter is not valid.
ENOTSOCK The s parameter refers to a file, not a
socket.
EOPNOTSUPP The referenced socket is not of type
SOCK_STREAM.
EFAULT The addr parameter is not in a writable
part of the user address space.
EWOULDBLOCK The socket is marked non-blocking, and
no connections are present to be
accepted.
Related Information
In this book: "bind," "connect," "listen," "select,"
and "socket."