connect(2)connect(2)NAMEconnect - initiate a connection on a socket
SYNOPSIS
AF_CCITT only
AF_INET, AF_INET6 and AF_VME_LINK only
AF_UNIX only
UNIX 03 only (X/Open Sockets)
Obsolescent UNIX 95 only (X/Open Sockets)
DESCRIPTION
The 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 cor‐
rect socket address structure should be used with each address family
(for example, for AF_INET and AF_VME_LINK, for AF_INET6, and for
AF_UNIX). Typically, the function is used to pass this value (for
example,
If the socket is of type 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 attempts to contact the remote host to make a
connection between the remote socket (peer) and the local socket speci‐
fied by s. The call normally blocks until the connection completes.
If nonblocking mode has been enabled with the or flags or the request
and the connection cannot be completed immediately, returns an error as
described below. In these cases, can be used on this socket to deter‐
mine when the connection has completed by selecting it for writing.
The system call may complete if remote program has a pending even
though remote program had not yet issued an system call.
and are defined in and explained in fcntl(2), fcntl(5), and socket(7).
is defined in and explained in ioctl(2), ioctl(5), and socket(7).
If s is a socket that is bound to the same local address as another
socket, returns if addr is the same as the peer address of that other
socket. This situation can only happen if the 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)), 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 multiple times to change the peer address. For data‐
gram sockets, a side effect of attempting to connect to some invalid
address (see 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 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 call
before issuing the call.
X/Open Sockets Compilation Environment
See xopen_networking(7).
DEPENDENCIES
AF_CCITT
The option to is not supported for sockets in the AF_CCITT address fam‐
ily.
RETURN VALUE
returns the following values:
Successful completion.
Failure.
is set to indicate the error.
ERRORS
If fails, 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
or 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
or and the connection cannot be completed
immediately. This is not a failure.
Make the call again a few seconds later.
Alternatively, wait for completion by
calling 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 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 informa‐
tion.
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 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 has failed.
The remote system or an intermediate system in the communica‐
tions path
does not support a protocol option sent
by the local system. This option may
have been set using a or 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 an attempt is made to
issue a call on a socket.
Connection establishment timed out without establishing a con‐
nection.
One reason could be that the connection
requests queue at the remote socket may
be full (see listen(2)).
OBSOLESCENCE
Currently, the and types are the same size. This is compatible with
the UNIX 95 and UNIX 03 profiles. However, in a future release, might
be a different size, but that should not adversely affect application
behavior in this case. Applications may use now. But applications
that need to be portable to the UNIX 95 profile should follow the
X/Open specification (see xopen_networking(7)).
WARNINGS
Linking binary objects compiled to specification and binary objects
compiled to specification to the same executable may result in unex‐
pected behavior, including application abnormal termination and unex‐
pected socket errors. See xopen_networking(7) for details and remedy.
FUTURE DIRECTION
Currently, the default behavior is the however, it might be changed to
in a future release. At that time, any behavior that is incompatible
with might be obsoleted. Applications that conform to the X/Open spec‐
ification now will avoid migration problems (see xopen_networking(7)).
AUTHOR
was developed by HP and the University of California, Berkeley.
SEE ALSOaccept(2), bind(2), fcntl(2), getsockname(2), getsockopt(2), ioctl(2),
listen(2), select(2), socket(2), fcntl(5), ioctl(5), privileges(5),
thread_safety(5), socket(7), xopen_networking(7).
STANDARDS CONFORMANCEconnect(2)