z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


accept()

z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
SC27-3660-00

The accept() call is used by a server to accept a connection request from a client. The call accepts the first connection on its queue of pending connections. The accept() call creates a new socket descriptor with the same properties as s and returns it to the caller. 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 -1 and sets errno to EWOULDBLOCK. The new socket descriptor cannot be used to accept new connections. The original socket, s, remains available to accept additional connection requests.

#include <manifest.h>
#include <bsdtypes.h>
#include <socket.h>
#include <in.h>
int accept(int s, struct sockaddr *addr, int *addrlen)
Parameter
Description
s
The socket descriptor.
addr
The socket address of the connecting client that is filled by accept() before it returns. The format of addr is determined by the domain in which the client resides. addr is specified by accept() only when both addr and addrlen are nonzero values.
addrlen
Must initially point to an integer that contains the size in bytes of the storage pointed to by addr. If addr is NULL, then addrlen is ignored and can be NULL.

The s parameter is a stream socket descriptor created using the socket() call. It is usually bound to an address using the bind() 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 addr parameter points to a buffer into which the connection requester address is placed. The addr parameter is optional and can be set to NULL. If addr or addrlen is null or 0, addr is not specified. The exact format of addr depends on the addressing domain from which the communication request originated. For example, if the connection request originated in the AF_INET domain, addr points to a sockaddr_in structure as defined in the header file IN.H. The addrlen parameter is used only when name is not NULL. Before calling accept(), you must set the integer pointed to by addrlen to the size of the buffer, in bytes, pointed to by addr. If the buffer is not large enough to hold the address, only the addrlen number of bytes of the requester address is copied.
Note: This call is used only with SOCK_STREAM sockets. There is no way to screen requesters without calling accept(). The application cannot determine which system from which requesters connections will be accepted. However, the caller can choose to close a connection immediately after discovering the identity of the requester.

A socket can be checked for incoming connection requests using the select() call.

Return values

A nonnegative socket descriptor indicates success; the value -1 indicates an error. Errno identifies the specific error.
Errno
Description
EBADF
The s parameter is not a valid socket descriptor.
ENOBUFS
Indicates insufficient buffer space available to create the new socket.
EINVAL
The s parameter is not of type SOCK_STREAM.
EFAULT
Using addr and addrlen would result in an attempt to copy the address into a portion of the caller address space to which information cannot be written.
EWOULDBLOCK
The socket descriptor s is in nonblocking mode, and no connections are in the queue.

Example

Following are two examples of the accept() call. In the first, the caller wants to have the requester’s address returned. In the second, the caller does not want the requester address returned’s.
int clientsocket;
int s;
struct sockaddr clientaddress;
int addrlen;
int accept(int s, struct sockaddr *addr, int *addrlen);
/* 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() */
addrlen = 0;
clientsocket = accept(s, (struct sockaddr *) 0, (int *) 0);

Related calls

bind(), connect(), getpeername(), listen(), socket()

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014