Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > C


HP-UX 11i Version 3: February 2007

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index


connect — initiate a connection on a socket


#include <sys/socket.h>


#include <x25/x25addrstr.h>


#include <netinet/in.h>

AF_UNIX only

#include <sys/un.h>

int connect(int s, const void *addr, int addrlen);

UNIX 03 only (X/Open Sockets)

int connect(int s, const struct sockaddr *addr, socklen_t addrlen);

Obsolescent UNIX 95 only (X/Open Sockets)

int connect(int s, const struct sockaddr *addr, size_t addrlen);


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.

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 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

See xopen_networking(7).



The SO_REUSEADDR option to setsockopt() is not supported for sockets in the AF_CCITT address family.


connect() returns the following values:


Successful completion.


Failure. errno is set to indicate the error.


If connect() fails, errno is set to one of the following values:


The specified address is already in use.

For datagram sockets, the peer address is no longer maintained by the system.


The specified address is not available on this machine, or the socket is a TCP/UDP socket and the zero port number is specified.

For datagram sockets, the peer address is no longer maintained by the system.


The specified address is not a valid address for the address family of this socket.

For datagram sockets, the peer address is no longer maintained by the system.


Nonblocking I/O is enabled with O_NONBLOCK, O_NDELAY, or FIOSNBIO, and a previous connection attempt has not yet completed.


s is not a valid file descriptor.


The attempt to connect was forcefully rejected.


addr is not a valid pointer.


Nonblocking 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() call again a few seconds later. Alternatively, wait for completion by calling select() and selecting for write.


The connect was interrupted by 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.


The socket has already been shut down; addrlen is a bad value; the X.121 address length is zero, negative, or greater than 15 digits.

For datagram sockets, if addrlen is a bad value, the peer address is no longer maintained by the system.


The socket is already connected.


The X.25 interface specified in the addr struct was found 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.


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, connections are loose on the interface hoods at the modem, the modem failed, or noise interfered with the line for an extremely long period of time.


No buffer space is available. The connect() has failed.


The x25ifname field refers to a nonexistent interface.


The file or the directory path specified in the target address for AF_UNIX domain does not exist.


No memory is available. The connect() has failed.


The remote system or an intermediate system in the communications path does not support a protocol option sent by the local system. This option may have been set using a getsockopt() or setsockopt() call, or set as a system parameter.


All available virtual circuits are in use.


s is a valid file descriptor, but it is not a socket.


The socket referenced by s does not support connect(); an attempt is made to issue a connect() call on a listen() socket.


Connection establishment timed out without establishing a connection. One reason could be that the connection requests queue at the remote socket may be full (see listen(2)).


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)).


connect() was developed by HP and the University of California, Berkeley.


connect(): XPG4, UNIX 95, UNIX 03

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.