gtpc1m3hTransmission Control Protocol/Internet Protocol

accept -- Accept a Connection Request

The accept function is used by a server to accept a connection request from a client.

Format

#include <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 Appendix C, Socket Error Return Codes for more information about socket errors.

Note:
Unless otherwise stated in the description, the following error codes can be returned for either TCP/IP offload support or TCP/IP native stack support.

Value
Description

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.

SOCNOBUFS
There is not enough buffer space available to create the new socket. This error code is returned only for TCP/IP offload support.

SOCSOCKTNOSUPPORT
The s parameter is not of type SOCK_STREAM.

SOCFAULT
Using addr and addrlen would result in an attempt to copy the address into a protected address space.

SOCWOULDBLOCK
The socket s is in nonblocking mode and no connections are in the queue.

SOCCONNABORTED
The software caused a connection abend. This error code is returned only for TCP/IP offload support.

EIBMIUCVERR
The accept function was not successful because an error was received from the offload device. This error code is returned only for TCP/IP offload support.

E1052STATE
The socket was closed because the system was in or cycling down to 1052 state.

EINACT
All offload devices associated with the socket descriptor have been disconnected. The socket is closed. This error code is returned only for TCP/IP offload support.

EINACTWS
An offload device associated with the socket descriptor has been disconnected. The socket is still available. This error code is returned only for TCP/IP offload support.

ESYSTEMERROR
A system error has occurred and closed the socket.

SOCTIMEDOUT
The operation timed out. The socket is still available. This error code is returned only for TCP/IP native stack support.

Programming Considerations

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 <socket.h>

·
·
·
int newclient_sock; int server_sock; struct sockaddr client_addr; int addrlen; /* socket, bind, and listen have been called */

  1. I want the address now:
    addrlen = sizeof(client_addr);
    newclient_sock = accept(server_sock, &client_addr, &addrlen);
    
  2. I can get the address later using getpeername:
    addrlen = 0;
    newclient_sock = accept(server_sock, (struct sockaddr *) 0, (int *) 0);
    

Related Information