CONNECT(3)                 POSIX Programmer's Manual                CONNECT(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       connect -- connect a socket

SSYYNNOOPPSSIISS
       #include <sys/socket.h>

       int connect(int _s_o_c_k_e_t, const struct sockaddr *_a_d_d_r_e_s_s,
           socklen_t _a_d_d_r_e_s_s___l_e_n);

DDEESSCCRRIIPPTTIIOONN
       The _c_o_n_n_e_c_t() function shall attempt to make a connection on a
       connection-mode socket or to set or reset the peer address of a
       connectionless-mode socket. The function takes the following arguments:

       _s_o_c_k_e_t      Specifies the file descriptor associated with the socket.

       _a_d_d_r_e_s_s     Points to a ssoocckkaaddddrr structure containing the peer address.
                   The length and format of the address depend on the address
                   family of the socket.

       _a_d_d_r_e_s_s___l_e_n Specifies the length of the ssoocckkaaddddrr structure pointed to
                   by the _a_d_d_r_e_s_s argument.

       If the socket has not already been bound to a local address, _c_o_n_n_e_c_t()
       shall bind it to an address which, unless the socket's address family
       is AF_UNIX, is an unused local address.

       If the initiating socket is not connection-mode, then _c_o_n_n_e_c_t() shall
       set the socket's peer address, and no connection is made. For
       SOCK_DGRAM sockets, the peer address identifies where all datagrams are
       sent on subsequent _s_e_n_d() functions, and limits the remote sender for
       subsequent _r_e_c_v() functions. If the _s_a___f_a_m_i_l_y member of _a_d_d_r_e_s_s is
       AF_UNSPEC, the socket's peer address shall be reset. Note that despite
       no connection being made, the term ``connected'' is used to describe a
       connectionless-mode socket for which a peer address has been set.

       If the initiating socket is connection-mode, then _c_o_n_n_e_c_t() shall
       attempt to establish a connection to the address specified by the
       _a_d_d_r_e_s_s argument. If the connection cannot be established immediately
       and O_NONBLOCK is not set for the file descriptor for the socket,
       _c_o_n_n_e_c_t() shall block for up to an unspecified timeout interval until
       the connection is established. If the timeout interval expires before
       the connection is established, _c_o_n_n_e_c_t() shall fail and the connection
       attempt shall be aborted. If _c_o_n_n_e_c_t() is interrupted by a signal that
       is caught while blocked waiting to establish a connection, _c_o_n_n_e_c_t()
       shall fail and set _e_r_r_n_o to [[EEIINNTTRR]], but the connection request shall
       not be aborted, and the connection shall be established asynchronously.

       If the connection cannot be established immediately and O_NONBLOCK is
       set for the file descriptor for the socket, _c_o_n_n_e_c_t() shall fail and
       set _e_r_r_n_o to [[EEIINNPPRROOGGRREESSSS]], but the connection request shall not be
       aborted, and the connection shall be established asynchronously.
       Subsequent calls to _c_o_n_n_e_c_t() for the same socket, before the
       connection is established, shall fail and set _e_r_r_n_o to [[EEAALLRREEAADDYY]].

       When the connection has been established asynchronously, _p_s_e_l_e_c_t(),
       _s_e_l_e_c_t(), and _p_o_l_l() shall indicate that the file descriptor for the
       socket is ready for writing.

       The socket in use may require the process to have appropriate
       privileges to use the _c_o_n_n_e_c_t() function.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, _c_o_n_n_e_c_t() shall return 0; otherwise, -1
       shall be returned and _e_r_r_n_o set to indicate the error.

EERRRROORRSS
       The _c_o_n_n_e_c_t() function shall fail if:

       EEAADDDDRRNNOOTTAAVVAAIILL
              The specified address is not available from the local machine.

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

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

       EEBBAADDFF  The _s_o_c_k_e_t argument is not a valid file descriptor.

       EECCOONNNNRREEFFUUSSEEDD
              The target address was not listening for connections or refused
              the connection request.

       EEIINNPPRROOGGRREESSSS
              O_NONBLOCK is set for the file descriptor for the socket and the
              connection cannot be immediately established; the connection
              shall be established asynchronously.

       EEIINNTTRR  The attempt to establish a connection was interrupted by
              delivery of a signal that was caught; the connection shall be
              established asynchronously.

       EEIISSCCOONNNN
              The specified socket is connection-mode and is already
              connected.

       EENNEETTUUNNRREEAACCHH
              No route to the network is present.

       EENNOOTTSSOOCCKK
              The _s_o_c_k_e_t argument does not refer to a socket.

       EEPPRROOTTOOTTYYPPEE
              The specified address has a different type than the socket bound
              to the specified peer address.

       EETTIIMMEEDDOOUUTT
              The attempt to connect timed out before a connection was made.

       If the address family of the socket is AF_UNIX, then _c_o_n_n_e_c_t() shall
       fail if:

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

       EELLOOOOPP  A loop exists in symbolic links encountered during resolution of
              the pathname in _a_d_d_r_e_s_s.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_MAX}.

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

       EENNOOTTDDIIRR
              A component of the path prefix of the pathname in _a_d_d_r_e_s_s names
              an existing file that is neither a directory nor a symbolic link
              to a directory, or the pathname in _a_d_d_r_e_s_s contains at least one
              non-<slash> character and ends with one or more trailing <slash>
              characters and the last pathname component names an existing
              file that is neither a directory nor a symbolic link to a
              directory.

       The _c_o_n_n_e_c_t() function may fail if:

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

       EEAADDDDRRIINNUUSSEE
              Attempt to establish a connection that uses addresses that are
              already in use.

       EECCOONNNNRREESSEETT
              Remote host reset the connection request.

       EEHHOOSSTTUUNNRREEAACCHH
              The destination host cannot be reached (probably because the
              host is down or a remote router cannot reach it).

       EEIINNVVAALL The _a_d_d_r_e_s_s___l_e_n argument is not a valid length for the address
              family; or invalid address family in the ssoocckkaaddddrr structure.

       EELLOOOOPP  More than {SYMLOOP_MAX} symbolic links were encountered during
              resolution of the pathname in _a_d_d_r_e_s_s.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a pathname exceeds {PATH_MAX}, or pathname
              resolution of a symbolic link produced an intermediate result
              with a length that exceeds {PATH_MAX}.

       EENNEETTDDOOWWNN
              The local network interface used to reach the destination is
              down.

       EENNOOBBUUFFSS
              No buffer space is available.

       EEOOPPNNOOTTSSUUPPPP
              The socket is listening and cannot be connected.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       None.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       If _c_o_n_n_e_c_t() fails, the state of the socket is unspecified. Conforming
       applications should close the file descriptor and create a new socket
       before attempting to reconnect.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _a_c_c_e_p_t(), _b_i_n_d(), _c_l_o_s_e(), _g_e_t_s_o_c_k_n_a_m_e(), _p_o_l_l(), _p_s_e_l_e_c_t(), _s_e_n_d(),
       _s_h_u_t_d_o_w_n(), _s_o_c_k_e_t()

       The Base Definitions volume of POSIX.1-2017, <<ssyyss__ssoocckkeett..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                           CONNECT(3)
