#include <sys/types.h> #include <sys/socket.h> int connect(int socket_descriptor, struct sockaddr *destination_address, int address_length)
#define _XOPEN_SOURCE 520 #include <sys/socket.h> int connect(int socket_descriptor, const struct sockaddr *destination_address, socklen_t address_length)
The connect() function is used to establish a connection on a connection-oriented socket or establish the destination address on a connectionless socket.
There are two versions of the API, as shown above. The base i5/OS 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.
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.
When the address type of the socket identified by the socket_descriptor is AF_INET and is running IP over SNA, the thread must have retrieve, insert, delete, and update authority to the APPC device. When the thread does not have this level of authority, then an errno of EACCES is returned.
connect() returns an integer. Possible values are:
When a connect() fails, errno can be set to one of the following. For additional debugging information, see Debugging IP over SNA Configurations.
[EACCES] | Permission denied.
This error code indicates one of the following:
|
[EADDRINUSE] | Address already in use.
This error code indicates one of the following:
|
[EADDRNOTAVAIL] | Address not available.
This error code is returned if the socket_descriptor parameter points to a socket with an address family of AF_INET or AF_INET6 and either a port was not available or a route to the address specified by the destination_address parameter could not be found. |
[EAFNOSUPPORT] | The type of socket is not supported in this
protocol family.
The address family specified in the address structure pointed to by destination_address parameter cannot be used with the socket pointed to by the socket_descriptor parameter. |
[EALREADY] | Operation already in progress.
A previous connect() function had already been issued for the socket pointed to by the socket_descriptor parameter, and has yet to be completed. This error code is returned only on sockets that use a connection-oriented transport service. |
[EBADF] | Descriptor not valid. |
[ECONNREFUSED] | The destination socket refused an attempted
connect operation.
This error occurs when there is no application that is bound to the address specified by the destination_address parameter. |
[EFAULT] | Bad address.
The system detected an address which was not valid while attempting to access the destination_address parameter. |
[EHOSTUNREACH] | A route to the remote host is not available.
This error code is returned on sockets that use the AF_INET and AF_INET6 address families. |
[EINPROGRESS] | Operation in progress.
The socket_descriptor parameter points to a socket that is marked as nonblocking and the connection could not be completed immediately. This error code is returned only on sockets that use a connection-oriented transport service. |
[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.
This error code is returned only on sockets that use a connection-oriented transport service. |
[ELOOP] | A loop exists in symbolic links encountered
during pathname resolution.
This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID address family. |
[ENAMETOOLONG] | File name too long.
This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID address family. |
[ENETDOWN] | The network is not currently available. |
[ENETUNREACH] | Cannot reach the destination network.
This error code is returned for sockets that use the AF_INET or AF_INET6 address families, the address specified by the destination_address parameter requires the use of a router, and the socket option SO_DONTROUTE is currently set on. |
[ENOBUFS] | There is not enough buffer space for the requested operation. |
[ENOENT] | No such file or directory.
This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID address family. |
[ENOSYS] | Function not implemented.
This error code is only returned on sockets that use the AF_UNIX and AF_UNIX_CCSID address families. |
[ENOTDIR] | Not a directory. |
[ENOTSOCK] | The specified descriptor does not reference a socket.
This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID address family. |
[EOPNOTSUPP] | Operation not supported.
connect() is not allowed on a passive socket (a socket for which a listen() has been done). |
[EPROTOTYPE] | The socket type or protocols are not compatible.
This error code is only returned on sockets that use the AF_UNIX or AF_UNIX_CCSID address family. |
[ETIMEDOUT] | A remote host did not respond within the timeout
period.
This error code is returned when connection establishment times out. No connection is established. A possible cause may be that the partner application is bound to the address specified by the destination_address parameter, but the partner application has not yet issued a listen(). |
[EUNKNOWN] | Unknown system state. |
[EUNATCH] | The protocol required to support the specified address family is not available at this time. |
[EPROTO] | An underlying protocol error has occurred. |
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. |
Note: Issuing connect() on sockets of type SOCK_DGRAM and SOCK_RAW is not recommended because of dynamic route reassignment (picking a new route when a route that was previously used is no longer available). When this reassignment occurs, the next packet from the partner program can be received from a different IP address than the address your application specified on the connect(). This results in the data being discarded.
Top | UNIX-Type APIs | APIs by category |