connect(3N-xs) (X/OPEN SOCKETS) connect(3N-xs)
NAME
connect - connect a socket
SYNOPSIS
cc [flag ...] file ... -lxnet [library ...]
#include <sys/socket.h>
int connect(int socket, const struct sockaddr *address,
sizet addresslen);
DESCRIPTION
The connect function requests a connection to be made on a socket. The
function takes the following arguments:
socket Specifies the file descriptor associated with the
socket.
address Points to a sockaddr structure containing the peer
address. The length and format of the address depend
on the address family of the socket.
addresslen Specifies the length of the sockaddr structure pointed
to by the address argument.
If the initiating socket is not connection-oriented, then connect sets
the socket's peer address, but no connection is made. For SOCKDGRAM
sockets, the peer address identifies where all datagrams are sent on
subsequent send calls, and limits the remote sender for subsequent
recv 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 the address
argument.
If the connection cannot be established immediately and ONONBLOCK is
not set for the file descriptor for the socket, connect will block for
up to an unspecified timeout interval until the connection is esta-
blished. 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 establish a connection, 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 ONONBLOCK 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 esta-
blished, will fail and set errno to EALREADY.
Page 1 Reliant UNIX 5.44 Printed 11/98
connect(3N-xs) (X/OPEN SOCKETS) connect(3N-xs)
When the connection has been established asynchronously, select and
poll will indicate that the file descriptor for the socket is ready
for writing.
RETURN VALUE
Upon successful completion, connect returns 0. Otherwise, -1 is
returned and errno is set to indicate the error.
ERRORS
The connect function will fail if:
EADDRNOTAVAIL The specified address is not available from the local
machine.
EAFNOSUPPORT The specified address is not a valid address for the
address family of the specified socket.
EALREADY A connection request is already in progress for the
specified socket.
EBADF The socket argument is not a valid file descriptor.
ECONNREFUSED The target address was not listening for connections
or refused the connection request.
EINPROGRESS ONONBLOCK is set for the file descriptor for the
socket and the connection cannot be immediately esta-
blished; the connection will be established asynchro-
nously.
EINTR The attempt to establish a connection was interrupted
by delivery of a signal that was caught; the connec-
tion will be established asynchronously.
EISCONN The specified socket is connection-oriented and is
already connected.
ENETUNREACH No route to the network is present.
ENOTSOCK The socket argument does not refer to a socket.
EPROTOTYPE The specified address has a different type than the
socket bound to the specified peer address.
ETIMEDOUT The attempt to connect timed out before a connection
was made.
Page 2 Reliant UNIX 5.44 Printed 11/98
connect(3N-xs) (X/OPEN SOCKETS) connect(3N-xs)
If the address family of the socket is AFUNIX, then connect will fail
if:
ENOTDIR A component of the path prefix of the pathname in
address is not a directory.
ENAMETOOLONG A component of a pathname exceeded {NAMEMAX} charac-
ters, or an entire pathname exceeded {PATHMAX} char-
acters.
EACCES Search permission is denied for a component of the
path prefix; or write access to the named socket is
denied.
EIO An I/O error occurred while reading from or writing to
the file system.
ELOOP Too many symbolic links were encountered in translat-
ing the pathname in address.
ENOENT A component of the pathname does not name an existing
file or the pathname is an empty string.
The connect function may fail if:
EADDRINUSE Attempt to establish a connection that uses addresses
that are already in use.
ECONNRESET Remote host reset the connection request.
EHOSTUNREACH The destination host cannot be reached (probably
because the host is down or a remote router cannot
reach it).
EINVAL The addresslen argument is not a valid length for the
address family; or invalid address family in sockaddr
structure.
ENAMETOOLONG Pathname resolution of a symbolic link produced an
intermediate result whose length exceeds {PATHMAX}.
ENETDOWN The local interface used to reach the destination is
down.
ENOBUFS No buffer space is available.
ENOSR There were insufficient STREAMS resources available to
complete the operation.
EOPNOTSUPP The socket is listening and can not be connected.
Page 3 Reliant UNIX 5.44 Printed 11/98
connect(3N-xs) (X/OPEN SOCKETS) connect(3N-xs)
APPLICATION USAGE
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.
SEE ALSO
close(2), poll(2), select(3C), accept(3N-xs), bind(3N-xs),
getsockname(3N-xs), send(3N-xs), shutdown(3N-xs), socket(3N-xs).
Page 4 Reliant UNIX 5.44 Printed 11/98