#include <cafe.h>
#include <cafe/network.h>

int SOAccept(int fd, struct sockaddr *addr, int *addrlen);


fd Socket descriptor created with SOSocket.
addr Pointer to a sockaddr structure.
addrlen Initially contains the size of the structure pointed to by addr; upon return will contain the actual length of the address returned (in bytes).

Return Values

A descriptor for the accepted socket that is a non-negative integer. Upon an error, -1 is returned, and errno is set appropriately.


SO_EBUSY Socket resource manager busy processing requests.
SO_ELIBNOTREADY Socket library is not initialized.
SO_ENOMEM Insufficient memory in the stack.
SO_EMFILE Too many sockets open. No more socket file descriptors available.
SO_EINVAL - SOListen has not been called on the socket descriptor fd.
- If addr is not NULL and either addrlen is NULL or contains an invalid length (i.e., length is not equal to sizeof(struct sockaddr_in)).
SO_EWOULDBLOCK The socket is marked non-blocking and no connections are present to be accepted.
SO_ECONNABORTED A connection arrived, but it was closed while waiting on the listen queue.
SO_EABORTED The operation was aborted. When a socket is closed, operations that are blocked on the socket are aborted.
SO_EUNKNOWN Unknown error.
SO_ENOTSOCK Specified argument is not a socket.
SO_ERANGEINVALID Internal API error, invalid error code.
SO_EAPIERROR Internal API error.


The argument fd is a socket that has been created with SOSocket, is bound to an address with SOBind and is listening for connections after a SOListen. SOAccept takes the first connection out of the queue of pending connections, creates a new socket with the same properties as s and allocates a new file descriptor for the socket. If no pending connections are present on the queue, and the socket is not marked as non-blocking, SOAccept blocks the caller until a connection is present. If the socket is marked non-blocking and no pending connections are present on the queue, SOAccept returns an error as described above. The accepted socket is used to read and write data to and from the socket which connected to this one; it is not used to accept more connections. The original socket s remains open for accepting further connections.

The argument addr is a result parameter that is filled in with the address of the connecting entity as known to the communications layer; i.e., the exact format of the addr parameter is determined by the domain in which the communication is occurring. addrlen 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. If addr is NULL, nothing is filled in; in this case, addrlen is not used, and should also be NULL. This call is used with connection-based socket types, currently with SOCK_STREAM.

It is possible to SOSelect a socket for the purposes of performing an SOAccept by selecting it for read.

Do Not Call From

Callbacks Do not call this function from any callback function.
Interrupt handler Do not call this function from any interrupt handler.
Exception handler Do not call this function from any exception handler.

See Also


Revision History

2012/10/09 Updated Errors section.
2012/08/15 Cleanup pass.
2011/07/11 Added Errors section.
2011/02/21 Initial version.