accept()

The accept() socket call accepts a connection request from a remote host. Raccept() accepts a connection request from a SOCKS server. See Socket Secure Support for information about SOCKS.

Syntax

#include <types.h>
#include <sys\socket.h>
#include <netinet\in.h>
#include <netnb\nb.h>
#include <sys\un.h>
int accept(s, name, namelen)
int s;
sockaddr *name;
int *namelen;

Parameters

s

name namelen

Description

This call is used by a server acting in a connection-oriented mode to accept a connection request from a client. The call accepts the first connection on its queue of pending connection requests. The accept() call creates a new socket descriptor with the same properties as s and returns it to the caller. The new socket descriptor cannot be used to accept new connections. The original socket, s, remains available to accept more connection requests.

If the queue has no pending connection requests, accept() blocks the caller unless s is in nonblocking mode. If no connection requests are queued and s is in nonblocking mode, accept() returns a value of -1 and sets the return code to SOCEWOULDBLOCK.

The s parameter must be a socket descriptor created with the socket() call. It is usually bound to an address with the bind() call and must be made capable of accepting connections with the listen() call. The listen() call marks the socket as one that accepts connections and allocates a queue to hold pending connection requests. The listen() call allows the caller to place an upper boundary on the size of the queue.

The name parameter is a pointer to a buffer where the connection requester address is placed. The name parameter is optional and can be set to be the NULL pointer. If set to NULL, the requester address is not copied into the buffer. The exact format of name depends on the communications domain where the communication request originated. For example, if the connection request originated in the internet domain, name points to a sockaddr_in structure as defined in the header file <NETINET\IN.H>.

The namelen parameter is used only if name is not NULL. Before calling accept(), you must set the integer pointed to by namelen to the size, in bytes, of the buffer pointed to by name. On successful return, the integer pointed to by namelen contains the actual number of bytes copied into the buffer. If the buffer is not large enough to hold the address, up to namelen bytes of the requester address are copied.

This call is used only with SOCK_STREAM or SOCK_SEQPACKET sockets. You cannot screen requesters without calling accept(). The application cannot tell the system the requesters it will accept connections from. The caller can, however, choose to close a connection immediately after discovering the identity of the requester.

The select() call can be used to check the socket for incoming connection requests.

Return Values

A non-negative socket descriptor indicates success; the value -1 indicates an error. You can get the specific error code by calling sock_errno() or psock_errno().

Error Code

SOCENOTSOCK SOCEFAULT SOCEINTR SOCEINVAL SOCENOBUFS SOCEOPNOTSUPP SOCEWOULDBLOCK SOCECONNABORTED

Examples

The following are two examples of the accept() call. In the first, the caller wants to have the requester address returned. In the second, the caller does not want to have the requester address returned.

int clientsocket;int s;
struct sockaddr clientaddress;
int addrlen;
int accept(int s, struct sockaddr *addr, int *addrlen); /* extracted from sys/socket.h */
/* socket(), bind(), and listen() have been called */
/* EXAMPLE 1: I want the address now */
addrlen = sizeof(clientaddress);
clientsocket = accept(s, &clientaddress, &addrlen);
/* EXAMPLE 2: I can get the address later using getpeername() */
clientsocket = accept(s, (struct sockaddr *) 0, (int *) 0);

Related Calls


[Back: Protocol-Independent C Sockets API]
[Next: accept_and_recv()]