connect(2)
Requires Optional LAN/X.25 Software
NAME
connect − initiate a connection on a socket
SYNOPSIS
#include <sys/socket.h>
AF_INET only:
#include <netinet/in.h>
AF_UNIX only:
#include <sys/un.h>
AF_CCITT only:
#include <x25/x25addrstr.h>
int connect(int s, const void ∗addr, int addrlen);
DESCRIPTION
The parameter s is a socket descriptor. addr is a pointer to a socket address structure. This structure contains the address of a remote socket to which a connection will be established. addrlen is the size of this address structure. Since the size of the socket address structure varies among socket address families, the correct socket address structure should be used with each address family (for example, struct sockaddr_in for AF_INET, and struct sockaddr_un for AF_UNIX). Typically, the sizeof function is used to pass this value (for example, sizeof(struct sockaddr_in)).
If the socket is of type SOCK_DGRAM, connect specifies the peer address to which messages are to be sent, and the call returns immediately. Furthermore, this socket can only receive messages sent from this address.
If the socket is of type SOCK_STREAM, connect attempts to contact the remote host in order to make a connection between the remote socket (peer) and the local socket specified by s. The call normally blocks until the connection completes. If non-blocking mode has been enabled using the O_NONBLOCK or O_NDELAY fcntl flags or the FIOSNBIO ioctl request and the connection cannot be completed immediately, connect returns an error as described below. In these cases, select(2) can be used on this socket to determine when the connection has completed by selecting it for writing.
O_NONBLOCK and O_NDELAY are defined in <sys/fcntl.h> and explained in fcntl(2), fcntl(5), and socket(7). FIOSNBIO is defined in <sys/ioctl.h> and explained in ioctl(2), ioctl(5), and socket(7).
If s is a SOCK_STREAM socket that is bound to the same local address as another SOCK_STREAM socket, connect returns EADDRINUSE if addr is the same as the peer address of that other socket. This situation can only happen if the SO_REUSEADDR option has been set on an AF_INET socket (see getsockopt(2)).
If the AF_INET socket does not already have a local name bound to it (see bind(2)), the connect call also binds the socket to a local address chosen by the system.
Generally, stream sockets may successfully connect only once; datagram sockets may use connect multiple times to change the peer address. For datagram sockets, a side effect of attempting to connect to some invalid address (see DIAGNOSTICS below) is that the peer address is no longer maintained by the system. An example of an invalid address for a datagram socket is addrlen set to 0 and addr set to any value.
AF_CCITT only:
Use the x25addrstr struct for the address structure. The caller must know the X.121 address of the DTE to which the connection is to be established, including any sub-addresses or protocol-IDs that may be needed. Refer to af_ccitt(7F) for a detailed description of the x25addrstr address structure. If address-matching by protocol-ID, specify the protocol-ID with the X25_WR_USER_DATA ioctl() call before issuing the connect() call. The X25_WR_USER_DATA ioctl() call is described in socketx25(7).
DEPENDENCIES
AF_CCITT:
The SO_REUSEADDR option to setsockopt(2) is not supported for sockets in the AF_CCITT address family.
RETURN VALUE
If the call is successful, 0 is returned. If it fails, −1 is returned and an error code is stored in errno.
DIAGNOSTICS
connect fails if:
[EBADF] s is not a valid file descriptor.
[ENOTSOCK] s is a file descriptor for a file, not a socket.
[EADDRNOTAVAIL] The specified address is not available on this machine, or the socket is a TCP or UDP socket and the zero port number is specified.
For datagram sockets, the peer address is no longer maintained by the system.
[EAFNOSUPPORT] Addresses in the specified address family cannot be used with this socket.
For datagram sockets, the peer address is no longer maintained by the system.
[EALREADY] Non-blocking I/O is enabled using O_NONBLOCK, O_NDELAY or FIOSNBIO, and a previous connection attempt has not yet completed.
[EISCONN] The socket is already connected.
[EINVAL] The socket has already been shut down, or has a listen active on it; addrlen is a bad value; an attempt was made to connect() an AF_UNIX socket to an NFS -mounted (remote) name; the X.121 address length is zero, negative, or greater than fifteen digits.
For datagram sockets, if addrlen is a bad value, the peer address is no longer maintained by the system.
[ETIMEDOUT] Connection establishment timed out without establishing a connection. backlog may be full (see listen(2)).
[ECONNREFUSED] The attempt to connect was forcefully rejected.
[ENETUNREACH] The network is not reachable from this host.
For AF_CCITT only: X.25 Level 2 is down. The X.25 link is not working: wires might be broken, or connections are loose on the interface hoods at the modem, or the modem failed, or noise interfered with the line for an extremely long period of time.
[EADDRINUSE] The address is already in use.
For datagram sockets, the peer address is no longer maintained by the system.
[EFAULT] addr is not a valid pointer.
[EINPROGRESS] Non-blocking I/O is enabled using O_NONBLOCK, O_NDELAY or FIOSNBIO, and the connection cannot be completed immediately. This is not a failure. Make the connect(2) call again a few seconds later. Alternatively, wait for completion by calling select(2), selecting for write.
[ENODEV] The x25ifname field refers to a non-existent interface.
[ENOSPC] All available virtual circuits are in use.
[ENETDOWN] The X.25 interface specified in the addr struct was found or but was not in the initialized state. x25ifname field name is an interface which has been shut down or never initialized or suffered a power failure which erased its state information.
[ENOBUFS] No buffer space is available. The connect has failed.
[EINTR] The connect was interrupted by delivery of a signal before the connect sequence was complete. The building of the connection still takes place, even though the user is not blocked on the connect call.
[EOPNOTSUPP] A connect attempt was made on a socket type which does not support this call. Under X.25 an attempt was made to issue a connect() call on a listen() socket.
AUTHOR
connect was developed by the University of California, Berkeley.
SEE ALSO
accept(2), select(2), socket(2), getsockname(2), socket(7), socketx25(7), af_ccitt(7F).
Hewlett-Packard Company — HP-UX Release 8.05: June 1991