Standards / Extensions | C or C++ | Dependencies |
---|---|---|
z/OS® UNIX | both |
#define _OPEN_SYS_SOCK_EXT2
#include <sys/socket.h>
int accept_and_recv(int socket, int *accept_socket,
struct sockaddr *remote_address,
socklen_t *remote_address_len,
struct sockaddr *local_address,
socklen_t *local_address_len,
void *buffer, size_t buffer_len);
The accept_and_recv() function extracts the first connection on the queue of pending connections. It either reuses the specified socket (if *accept_socket is not -1) or creates a new socket with the same socket type, protocol, and address family as the listening socket (if *accept_socket is -1). It then returns the first block of data sent by the peer and returns the local and remote socket addresses associated with the connection.
A value of -1 for *accept_socket indicates that the accepting socket should be assigned by the system and returned to the application in this parameter. It is recommended that a value of -1 be used on the first call to accept_and_recv(). For more details, see Usage notes.
If *accept_socket is not -1, the incoming connection will be accepted on the socket specified by *accept_socket. The system may choose to reuse a different socket. If it does, the system will change *accept_socket to reflect the socket actually used.
If remote_address is not a NULL pointer, the address of the peer for the accepted connection is stored in the sockaddr structure pointed to by remote_address, and the length of this address is stored in the object pointed to by remote_address_len. If the actual length of the address is greater than the length of the supplied socket address structure, the stored address will be truncated.
If local_address is not a NULL pointer, the address of the local socket associated with this connection is stored in the sockaddr structure pointed to by local_address, and the length of this address is stored in the object pointed to by local_address_len. If the actual length of the address is greater than the length of the supplied socket address structure, the stored address will be truncated.
Nonblocking mode is not supported for this function. If O_NONBLOCK is set on the socket file descriptor, the function will return with -1 and errno will be set to EOPNOTSUPP.
If the listen queue is empty of connection requests, accept_and_recv() will not return until an incoming connection is received.
If buffer is not NULL, accept_and_recv will not return until the first block of data on the connection has been received, otherwise accept_and_recv() returns 0 after the connection is established.
To take full advantage of the performance improvements offered by the accept_and_recv() function, a process/thread model different from the one where a parent accepts in a loop and spins off child process threads is needed. The parent/process thread is eliminated. Multiple worker processes/threads are created, and each worker process/thread then executes the accept_and_recv() function in a loop. The performance benefits of accept_and_recv() include fewer buffer copies, recycled sockets, and optimal scheduling.
If successful, accept_and_recv() returns the number of bytes (zero or more) stored in the buffer pointed to by the buffer argument. Zero can be returned when buffer is NULL or when the client closes the socket without sending any data.
A partial success is achieved with *accept_socket being assigned, a return value of -1 and errno set to one of the following values:
If unsuccessful, accept_and_recv() sets *accepted_socket to -1, returns -1 and sets errno to one of the following values: