accept: Accept a connection request
The accept socket function is used by a server to accept a connection request from a client.
Last updated
Added for PUT00
Format
#include <sys/socket.h>
int accept(int s,
struct sockaddr *addr,
int *addrlen); - s
- The socket descriptor. The s parameter is a stream socket descriptor created with the socket function. It is bound to an address with the bind function. The listen function marks the socket as one that accepts connections and allocates a queue to hold ending connection requests. The listen function allows the caller to place an upper boundary on the size of the queue.
- addr
- The socket address of the connecting client that is filled in by accept before it returns. The format of addr is determined by the domain in which the client resides. This parameter can be NULL if the caller is not interested in the address of the client. If the addr parameter is not NULL, it points to a sockaddr structure.
- addrlen
- Must initially point to an integer that contains the size, in bytes, of the storage pointed to by addr. On return, that integer contains the size of the data returned in the storage pointed to by addr. If addr is NULL, addrlen is ignored and can be NULL.
Normal return
A nonnegative socket descriptor indicates that the call was successful.
Error return
A socket descriptor of -1
indicates an error. You can get the specific error code by calling sock_errno. See z/TPF Transmission Control Protocol/Internet Protocol for more information
about socket errors.
- SOCNOTSOCK
- The s parameter is not a valid socket descriptor.
- SOCACCES
- The socket accept user exit has rejected an incoming connection request.
- SOCINVAL
- The listen function was not called for socket s or an incorrect length was passed on the addrlen parameter.
- SOCSOCKTNOSUPPORT
- The s parameter is not of type SOCK_STREAM.
- SOCWOULDBLOCK
- The socket s is in nonblocking mode and no connections are in the queue.
- E1052STATE
- The socket was closed because the system was in or cycling down to 1052 state.
- ESYSTEMERROR
- A system error has occurred and closed the socket.
- SOCTIMEDOUT
- The operation timed out. The socket is still available.ze
Programming considerations
- The accept function creates a new socket descriptor with the same properties as s and returns it to the caller. Do not use the new socket to accept new connections. The original socket, s, remains available to accept more connection requests.
- The accept function accepts the first connection on its queue of pending connections. 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 sock_errno to SOCWOULDBLOCK.
- The accept function is used only with SOCK_STREAM sockets. There is no way to screen requesters without calling accept. The application cannot tell the system from which requesters it accepts connections. However, the caller can choose to close a connection immediately after determining the identity of the requester. If a connection request is rejected by the socket accept user exit, the accept function returns -1 and sets sock_errno to SOCACCES.
- Check a socket for incoming connection requests by using the select for read function.
- The receive timeout value (the SO_RCVTIMEO setsockopt option) determines how long to wait for a remote client to connect before the accept function times out.
- The accept function cannot be issued if an activate_on_accept call is pending for the socket. These operations are mutually exclusive.
- The socket accept connection user exit is UACC.
Examples
Following are two examples of
the accept function.
In the first example, the caller wants to have the address of the
requester returned. In the second example, the caller does not want
to have the address of the requester returned.
#include <sys/socket.h>
⋮
int newclient_sock;
int server_sock;
struct sockaddr client_addr;
int addrlen;
/* socket, bind, and listen have been called */ - I want the address now:
addrlen = sizeof(client_addr); newclient_sock = accept(server_sock, &client_addr, &addrlen); - I can get the address later using getpeername:
addrlen = 0; newclient_sock = accept(server_sock, (struct sockaddr *) 0, (int *) 0);
Related information
- activate_on_accept: Activate a program when the client connects
- bind: Bind a local name to the socket
- connect: Request a connection to a remote host
- getpeername: Return the name of the peer
- getsockname: Return the name of the local socket
- listen: Complete binding, create connection request queue
- sock_errno: Return the error code set by a socket call
- socket: Create an endpoint for communication.
See z/TPF Transmission Control Protocol/Internet Protocol for information about z/TPF TCP/IP support.