Syntax
#include <sys/types.h>
#include <sys/socket.h>
int accept(int socket_descriptor,
struct sockaddr *address,
int *address_length)
Threadsafe: Yes
|
The accept() function is used to wait for connection
requests. accept() takes the first connection request on the queue of
pending connection requests and creates a new socket
to service the connection request.
accept() is used with connection-oriented socket types, such as
SOCK_STREAM.
Parameters
- socket_descriptor
- (Input) The descriptor of the socket on which to wait.
- address
- (Output) A pointer to a buffer of type struct sockaddr
in which the address from which the connection request was received
is stored. The structure sockaddr is defined in
<sys/socket.h>.
struct sockaddr {
u_short sa_family;
char sa_data[14];
};
The sa_family field identifies the
address family to which the address belongs,
and sa_data is the address whose format is
dependent on the address family.
- address_length
- (Input/output) This parameter
is a value-result field. The caller
passes a pointer to
the length of the address parameter. On return from
the call, address_length contains the actual length of the
address from which the connection request was received.
Return Value
accept() returns an integer. Possible values are:
- -1 (unsuccessful)
- n (successful), where n is a socket descriptor.
Error Conditions
When accept() fails, errno can be set to one of the following:
- [EACCES]
- Permission denied.
A connection indication request was
received on the socket referenced by the socket_descriptor parameter,
but the process that issued the
accept() did not have the appropriate privileges required
to handle the request. The connection indication request is
reset by the system.
- [EBADF]
- Descriptor not valid.
- [ECONNABORTED]
- Connection ended abnormally.
An accept() was issued on a socket
for which receives have been disallowed (due to a shutdown() call).
This could also be encountered if time elapsed since a successful
Rbind() is greater than the margin allowed by the associated
SOCKS server.
- [EFAULT]
- Bad address.
System detected an address which
was not valid while attempting to access the address or
address_length parameters.
- [EINTR]
- Interrupted function call.
- [EINVAL]
- Parameter not valid.
This error code indicates one of the following:
-
The address_length parameter is set to a value that is less than
zero, and the address parameter is set to a value other than
a NULL pointer.
-
A listen() has not been issued against the socket
referenced by the socket_descriptor parameter.
- [EIO]
- Input/output error.
- [EMFILE]
- Too many descriptions for this process.
- [ENFILE]
- Too many descriptions in system.
- [ENOBUFS]
- There is not enough buffer space for the requested operation.
- [ENOTSOCK]
- The specified descriptor does not reference a socket.
- [EOPNOTSUPP]
- Operation not supported.
The socket_descriptor parameter references a socket that does not support the
accept(). The accept() is only valid on sockets that
are connection-oriented (for example, type of SOCK_STREAM).
- [EUNATCH]
- The protocol required to support the specified address family is
not available at this time.
- [EUNKNOWN]
- Unknown system state.
- [EWOULDBLOCK]
- Operation would have caused the thread to be suspended.
Error Messages
- CPE3418 E
- Possible APAR condition or hardware failure.
- CPF9872 E
- Program or service program &1
in library &2 ended. Reason code &3.
- CPFA081 E
- Unable to set return value or error code.
Usage Notes
-
If the address parameter is set to a NULL pointer or the
address_length parameter points to an integer which has a
value that is equal to zero, the address from which the
connection request was received is not returned.
-
If the length of the address to be returned exceeds the length
of the address parameter, the returned address is
truncated.
-
The following are inherited by the descriptor returned by the
accept() call:
- All socket options with a level of SOL_SOCKET.
- The status flags:
- Blocking flag (set/reset either by the ioctl() call with the
FIONBIO request or by the fcntl() call with the F_SETFL command
and the status flag set to O_NONBLOCK).
- Asynchronous flag (set/reset either by the ioctl() call
with the FIOASYNC request or by the fcntl() call with the
F_SETFL command and the status flag set to FASYNC).
- The process ID or process group ID that is to
receive SIGIO or SIGURG signals (set/reset by either
the ioctl() call with the FIOSETOWN or the SIOCSPGRP request,
or by the fcntl() call with the F_SETOWN command).
-
Closing a socket causes any queued but unaccepted connection requests
to be
reset.
- If the socket is using an address family of AF_UNIX, the address
(which is a path name)
is returned in the default
coded character set identifier
(CCSID) currently in effect for
the job.
- If a successful Rbind() has been performed on the listening socket,
then a new
connection is not returned, but rather an inbound connection occurs
on
the same listening socket.
The descriptor number returned is different, but it
actually refers to the same connection referred to by the listening
socket.
Related Information