#include <sys/types.h> #include <sys/socket.h> int accept_and_recv(int listen_socket_descriptor, int *accept_socket_descriptor, struct sockaddr *remote_address, size_t *remote_address_length, struct sockaddr *local_address, size_t *local_address_length, void *buffer, size_t buffer_length)
#define _XOPEN_SOURCE 520 #include <sys/socket.h> int accept_and_recv(int listen_socket_descriptor, int *accept_socket_descriptor, struct sockaddr *remote_address, socklen_t *remote_address_length, struct sockaddr *local_address, socklen_t *local_address_length, void *buffer, size_t buffer_length)
The accept_and_recv() function is used to wait for an incoming connection request, receive the first message from the peer, and return the local and remote socket addresses associated with the connection.
accept_and_recv() is used with connection-oriented sockets that have an address family of AF_INET or AF_INET6 and a socket type of SOCK_STREAM.
The accept_and_recv() API is a combination of the accept(), getsockname(), and recv() socket APIs. Socket applications that use these three APIs can obtain improved performance by using accept_and_recv().
There are two versions of the API, as shown above. The base i5/OS(TM) API uses BSD 4.3 structures and syntax. The other uses syntax and structures compatible with the UNIX 98 programming interface specifications. You can select the UNIX 98 compatible interface with the _XOPEN_SOURCE macro.
If a pointer to a value of -1 is passed in for this parameter, a new descriptor in the process's descriptor table will be allocated for incoming connection. The socket descriptor for a new connection will be returned to the application by this parameter. It is recommended that a value of -1 be used on the first call to accept_and_recv(). See the Usage Notes for additional information.
The BSD 4.3 structure is:
struct sockaddr { u_short sa_family; char sa_data[14]; };
The BSD 4.4/UNIX 98 compatible structure is:
typedef uchar sa_family_t; struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; };
The BSD 4.4 sa_len field is the length of the address. The sa_family field identifies the address family to which the address belongs, and sa_data is the address whose format is dependent on the address family.
Note: See the usage notes about using different address families with sockaddr_storage.
The BSD 4.3 structure is:
struct sockaddr { u_short sa_family; char sa_data[14]; };
The BSD 4.4/UNIX 98 compatible structure is:
typedef uchar sa_family_t; struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; };
The BSD 4.4 sa_len field is the length of the address. The sa_family field identifies the address family to which the address belongs, and sa_data is the address whose format is dependent on the address family.
Note: See the usage notes about using different address families with sockaddr_storage.
If IP over SNA is being used, *CHANGE authority to the APPC device is required.
accept_and_recv() returns an integer. Possible values are:
When accept_and_recv() fails, errno can be set to one of the following:
[EACCES] | Permission denied.
A connection indication request was received on the socket referenced by the listen_socket_descriptor parameter, but the process that issued the accept_and_recv() call did not have the appropriate privileges required to handle the request. The connection indication request is reset by the system. |
[EBADF] | Descriptor not valid.
Either the listen_socket_descriptor or the descriptor pointed to by the accept_socket_descriptor parameter is not a valid socket descriptor. |
[ECONNABORTED] | Connection ended abnormally.
An accept_and_recv() was issued on a socket for which receive operations have been disallowed (due to a shutdown() call). |
[EFAULT] | Bad address.
System detected an address that was not valid while attempting to access the accept_socket_descriptor, remote_address, remote_address_length, local_address, local_address_length, or buffer parameter. |
[EINTR] | Interrupted function call. |
[EINVAL] | Parameter not valid.
This error code indicates one of the following:
|
[EIO] | Input/output error. |
[EISCONN] | A connection has already been established. |
[EMFILE] | Too many descriptions for this process. |
[ENFILE] | Too many descriptions in system. |
[ENOBUFS] | There is not enough buffer space for the
requested operation. |
[ENOTSOCK] | The specified descriptor does not reference a
socket.
Either the listen_socket_descriptor or the descriptor pointed to by the accept_socket_descriptor parameter is not a valid socket descriptor. |
[EOPNOTSUPP] | Operation not supported.
This error code indicates one of the following:
|
[EUNATCH] | The protocol required to support the specified
address family is not available at this time. |
[EUNKNOWN] | Unknown system state. |
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
CPFA081 E | Unable to set return value or error code. |
If an application specifies a pointer to an unbound and unconnected socket descriptor for the accept_socket_descriptor parameter that is the same address family and socket type as the listen_socket_descriptor, the accept_and_recv() function will try to use the accept_socket_descriptor for the incoming connection. If the accept_socket_descriptor cannot be used for the incoming connection, the descriptor for that socket will be closed and a new socket will be created for the incoming connection. The new socket may have a different descriptor number associated with it. This means that the value that is returned by the accept_socket_descriptor parameter may not be the same value that was specified by the application when the accept_and_recv() function was called.
The ability to reuse an existing socket is not supported on all platforms. Therefore, it is recommended that a pointer to a value of -1 be passed in for the accept_socket_descriptor parameter. If socket reuse is not supported and the send_file() API is called with the flags parameter set to SF_REUSE, the socket connection will be closed and the socket descriptor will be set to -1 by the send_file() API. If socket reuse is supported, then the connection will be closed and the socket descriptor will be reset so that it can be used again. Regardless of whether socket reuse is supported or not, the application can pass its socket descriptor variable into the accept_and_recv() function as the accept_socket_descriptor parameter.
The BSD 4.3 structure is:
#define _SS_MAXSIZE 304 #define _SS_ALIGNSIZE (sizeof (char*)) #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(sa_family_t)) #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(sa_family_t)+ _SS_PAD1SIZE + _SS_ALIGNSIZE)) struct sockaddr_storage { sa_family_t ss_family; char _ss_pad1[_SS_PAD1SIZE]; char* _ss_align; char _ss_pad2[_SS_PAD2SIZE]; };
The BSD 4.4/UNIX 98 compatible structure is:
#define _SS_MAXSIZE 304 #define _SS_ALIGNSIZE (sizeof (char*)) #define _SS_PAD1SIZE (_SS_ALIGNSIZE - (sizeof(uint8_t) + sizeof(sa_family_t))) #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(uint8_t) + sizeof(sa_family_t)+ _SS_PAD1SIZE + _SS_ALIGNSIZE)) struct sockaddr_storage { uint8_t ss_len; sa_family_t ss_family; char _ss_pad1[_SS_PAD1SIZE]; char* _ss_align; char _ss_pad2[_SS_PAD2SIZE]; };
Top | UNIX-Type APIs | APIs by category |