connect -- initiate a connection on a socket


cc [options] file -lsocket -lnsl
#include <sys/types.h>
#include <sys/socket.h>

int connect(int socket, const struct sockaddr *address, size_t address_len);


connect requests that a connection be made on a socket. If the initiating socket is not connection-oriented, then connect sets the socket's peer address but no connection is made. For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent send(SSC) calls, and limits the remote sender for subsequent recv(SSC) calls. If address is a null address for the protocol, the socket's peer address will be reset.

If the initiating socket is connection-oriented, then connect attempts to establish a connection to the address specified by address.

If the connection cannot be established immediately and O_NONBLOCK is not set for the file descriptor for the socket, connect will block for up to an unspecified timeout interval until the connection is established. If the timeout interval expires before the connection is established, connect will fail and the connection attempt will be aborted. If connect is interrupted by a signal that is caught while blocked waiting to connect, connect will fail and set errno to EINTR, but the connection request will not be aborted, and the connection will be established asynchronously.

If the connection cannot be established immediately and O_NONBLOCK is set for the file descriptor for the socket, connect will fail and set errno to EINPROGRESS, but the connection request will not be aborted, and the connection will be established asynchronously. Subsequent calls to connect for the same socket, before the connection is established, will fail and set errno to EALREADY.

When the connection has been established asynchronously, select(S) and poll(S) will indicate that the file descriptor for the socket is ready for writing. However, this is only possible if the socket STREAMS module is the topmost module on the protocol stack with a write service procedure. This will be the normal case.



Return values

If the connection or binding succeeds, then 0 is returned. Otherwise a -1 is returned and a more specific error code is stored in errno.


The call fails if:

The specified address is not available from the local machine.

Addresses in the specified address family cannot be used with this socket.

A connection request is already in progress for the specified socket.

socket is not a valid descriptor.

The attempt to connect was refused or the target address was not listening for connections.

The socket is non-blocking and the connection cannot be completed immediately; the connection will be established asynchronously.

The connection attempt was interrupted before any data arrived by the delivery of a signal. The connection will be established asynchronously.

The socket is connection-oriented, and is already connected.

The network is not reachable from this host.

socket does not refer to a socket.

The file referred to by address is a socket of a type other than the socket bound to the specified peer address.

Connection establishment timed out without establishing a connection.

The address is already in use.

The remote host reset the connection request.

address_len is not the size of a valid address for the specified address family, or invalid address family in sockaddr structure.

Pathname resolution of a symbolic link produced an intermediate result whose length exceeds PATH_MAX.

The local interface used to reach the destination is down.

No buffer space is available.

There were insufficient STREAMS resources available to complete the operation.
If the address family of the socket is AF_UNIX, connect will fail if:

A component of the path prefix of the pathname in address is not a directory.

A component of a pathname exceeded NAME_MAX characters or an entire pathname exceeded PATH_MAX.

Search permission is denied for a component of the path prefix or write access to the named socket is denied.

An I/O error occurred while reading from or writing to the file system.

Too many symbolic links were encountered in translating the pathname in address.

A component of the pathname does not name an existing file or the pathname is an empty string.


accept(SSC), close(S), getsockname(SSC), socket(SSC)

RFC 2133


The behavior of non-blocking connect for unix domain (AF_UNIX) sockets is determined by the value of the ss_connafunixndelay parameter set by inconfig(ADMN).

The type of address structure passed to connect depends on the address family. UNIX® domain sockets (address family AF_UNIX) require a socketaddr_un structure as defined in sys/un.h; Internet domain IPv4 sockets (address family AF_INET) require a sockaddr_in structure as defined in netinet/in.h; Internet domain IPv6 sockets (address family AF_INET6) require a sockaddr_in6 structure as defined in netinet/in.h. Other address families may require other structures. Use the structure appropriate to the address family; cast the structure address to a generic struct sockaddr * in the call to connect and pass the size of the structure in the length argument. If connect fails, the state of the socket is unspecified. Portable applications should close the file descriptor and create a new socket before attempting to reconnect.

The sockaddr structure has been modified from previous releases to support variable length sockets. The net result of this modification is that the family member has been shortened to 8 bits and a new 8-bit member inserted before it called len. For more information on the new sockaddr structures, see: unix(ADMP) and inet(ADMP).

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 - 01 June 2005