gtpc1m3h | Transmission Control Protocol/Internet Protocol |
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
- 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.
- For sockets using TCP/IP native stack support, 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.
- For sockets using TCP/IP native stack support, 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 <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