|HP-UX Reference > C
HP-UX 11i Version 3: February 2007
connect — initiate a connection on a socket
The connect() function initiates a connection on a socket.
s is a socket descriptor.
addr is a pointer to a socket address structure containing the address of a remote socket to which a connection is to 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 AF_VME_LINK, struct sockaddr_in6 for AF_INET6, 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 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 nonblocking mode has been enabled with 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() can be used on this socket to determine when the connection has completed by selecting it for writing.
The connect() system call may complete if remote program has a pending listen() even though remote program had not yet issued an accept() system call.
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 s, which is an AF_INET or AF_INET6 socket (see getsockopt(2)).
If the AF_INET or AF_INET6 socket does not already have a local address bound to it (see bind(2)), connect() also binds the socket to a local address chosen by the system.
An AF_VME_LINK socket always 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 ERRORS 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.
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 subaddresses or protocol IDs that may be needed. If address-matching by protocol ID, specify the protocol ID with the X25_WR_USER_DATA ioctl() call before issuing the connect() call.
X/Open Sockets Compilation Environment
connect() returns the following values:
If connect() fails, errno is set to one of the following values:
Currently, the socklen_t and size_t types are the same size. This is compatible with the UNIX 95 and UNIX 03 profiles. However, in a future release, socklen_t might be a different size, but that should not adversely affect application behavior in this case. Applications may use socklen_t now. But applications that need to be portable to the UNIX 95 profile should follow the X/Open specification (see xopen_networking(7)).
Linking binary objects compiled to X/Open Sockets specification and binary objects compiled to HP-UX BSD Sockets specification to the same executable may result in unexpected behavior, including application abnormal termination and unexpected socket errors. See xopen_networking(7) for details and remedy.
Currently, the default behavior is the HP-UX BSD Sockets; however, it might be changed to X/Open Sockets in a future release. At that time, any HP-UX BSD Sockets behavior that is incompatible with X/Open Sockets might be obsoleted. Applications that conform to the X/Open specification now will avoid migration problems (see xopen_networking(7)).