accept a connection on a socket
#include <sys/types.h> #include <sys/socket.h> int accept( int s, struct sockaddr *addr, int *addrlen );
The accept() function:
If no pending connections are present on the queue, and the socket isn't marked as nonblocking, accept() blocks the caller until a connection is present. If the socket is marked as nonblocking and no pending connections are present on the queue, accept() returns an error as described below. The accepted socket may not be used to accept more connections. The original socket s remains open.
The addr argument is a result parameter that's filled in with the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the domain in which the connection was made.
The addrlen argument is a value-result parameter. It should initially contain the amount of space pointed to by addr; on return it will contain the actual length (in bytes) of the address returned. This call is used with the connection-based socket type, SOCK_STREAM.
If you do a select() for read on an unconnected socket (on which a listen() has been done), the select() will indicate when a connect request has occurred. In this way, an accept() can be made that won't block. For more information, see the C Library Reference.
For certain protocols that require an explicit confirmation, accept() can be thought of as merely dequeuing the next connection request and not implying confirmation. Confirmation can be implied by a normal read or write on the new file descriptor, and rejection can be implied by closing the new socket.
You can obtain user-connection request data without confirming the connection by:
Or
Similarly, you can provide user-connection rejection information by issuing a sendmsg() call with only the control information, or by calling setsockopt().
A descriptor for the accepted socket, or -1 if an error occurs (in which case errno is set).
If an error occurs, errno could contain one of the following:
POSIX 1003.1g (draft)
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
bind(), connect(), listen(), socket()
select() in the C Library Reference