HPlogo Berkeley Sockets/iX Reference Manual: HP 3000 MPE/iX Computer Systems > Chapter 2 Socket System Calls

ACCEPT

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

C Interface

   AF_UNIX only:



        #include <sys/types.h>

        #include <sys/socket.h>

        #include <sys/un.h>

     

        int ns;

        ns=accept(s, addr, addrlen)

        int s;

        struct sockaddr_un *addr;

        int *addrlen;



   AF_INET only:



        #include <sys/types.h>

        #include <sys/socket.h>

        #include <netinet/in.h>

     

        int ns;

        ns=accept(s, addr, addrlen)

        int s;

        struct sockaddr_in *addr;

        int *addrlen;

Description

This call is used with connection-based socket types, such as SOCK_STREAM. The argument s is a socket descriptor created with socket, bound to an address with bind, and listening for connections after a listen. If pending connections are present on the queue, the accept call extracts the first connection on the queue, creates a new socket, and allocates a new file descriptor, ns, for the socket. If no pending connections are present on the queue and non-blocking mode has been enabled using the O_NONBLOCK or O_NDELAY fcntl flags (refer to fcntl section), accept returns an error as described in the Errors section below. If no pending connections are present on the queue and non-blocking mode has not been enabled, accept blocks the caller until a connection is present. The accepted socket, ns, cannot be used to accept more connections. The original socket s remains open. It is possible to determine if a listening socket has pending connection requests ready for an accept call by using select for reading.

The argument addr should point to a local socket address structure. The accept call fills in this structure with the address of the connecting entity, as known to the underlying protocol. The format of the address depends upon the protocol and the address family of the socket s. The addrlen is a pointer to int; it should initially contain the size of the structure pointed to by addr. On return it contains the actual length (in bytes) of the address returned. If the memory pointed to by addr is not large enough to contain the entire address, only the first addrlen bytes of the address are returned.

AF_UNIX only: The addr parameter to accept() is ignored.

Return Value

If the call is successful, a non-negative integer is returned, which is a descriptor for the accepted socket. If the call fails, a -1 is returned and an error code is stored in errno.

Errors

The following errors are returned by accept:

[EHOSTDOWN]

The networking subsystem has not been started or has been stopped.

[EBADF]

The file descriptor s is invalid.

[ENOTSOCK]

The argument s references a file, not a socket.

[EOPNOTSUPP]

The socket referenced by s is not of type SOCK_STREAM.

[EFAULT]

The addr parameter is not a valid pointer.

[EWOULDBLOCK]

Non-blocking I/O is enabled using O_NDELAY, and no connections are present to be accepted.

[EMFILE]

The maximum number of file descriptors for this process are already currently open.

[ENFILE]

The system's table of open files is full, and no more accept calls can be accepted, temporarily.

[ENOBUFS]

No buffer space is available. The accept cannot complete. The queued socket connect request is aborted.

[EINVAL]

The socket referenced by s is not currently a listen socket, or it has been shut down. A listen must be done before an accept is allowed. This is also returned if the length is less than zero.

[EINTR]

The call was interrupted by a signal before a valid connection arrived.

[EAGAIN]

Non-blocking I/O is enabled using O_NONBLOCK, and no connections are present to be accepted.

MPE/iX Specific

Connections are completely established in the accept call. The addr returned from accept when a connection is made in loopback is the loopback address (127.0.0.1). On HP-UX, the local host's IP address would be returned in this case.

Author

UCB (University of California at Berkeley)

See Also

bind, connect, listen, select, socket

Feedback to webmaster