1CONNECT(3P)                POSIX Programmer's Manual               CONNECT(3P)
2
3
4

PROLOG

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

NAME

12       connect — connect a socket
13

SYNOPSIS

15       #include <sys/socket.h>
16
17       int connect(int socket, const struct sockaddr *address,
18           socklen_t address_len);
19

DESCRIPTION

21       The connect() function shall attempt to make a connection on a  connec‐
22       tion-mode  socket  or to set or reset the peer address of a connection‐
23       less-mode socket. The function takes the following arguments:
24
25       socket      Specifies the file descriptor associated with the socket.
26
27       address     Points to a sockaddr structure containing the peer address.
28                   The  length and format of the address depend on the address
29                   family of the socket.
30
31       address_len Specifies the length of the sockaddr structure  pointed  to
32                   by the address argument.
33
34       If  the socket has not already been bound to a local address, connect()
35       shall bind it to an address which, unless the socket's  address  family
36       is AF_UNIX, is an unused local address.
37
38       If  the  initiating socket is not connection-mode, then connect() shall
39       set  the  socket's  peer  address,  and  no  connection  is  made.  For
40       SOCK_DGRAM sockets, the peer address identifies where all datagrams are
41       sent on subsequent send() functions, and limits the remote  sender  for
42       subsequent  recv()  functions.  If  the  sa_family member of address is
43       AF_UNSPEC, the socket's peer address shall be reset. Note that  despite
44       no  connection being made, the term ``connected'' is used to describe a
45       connectionless-mode socket for which a peer address has been set.
46
47       If the initiating  socket  is  connection-mode,  then  connect()  shall
48       attempt  to  establish  a  connection  to  the address specified by the
49       address argument. If the connection cannot be  established  immediately
50       and  O_NONBLOCK is not set for the file descriptor for the socket, con‐
51       nect() shall block for up to an unspecified timeout interval until  the
52       connection  is  established. If the timeout interval expires before the
53       connection is established, connect()  shall  fail  and  the  connection
54       attempt  shall be aborted. If connect() is interrupted by a signal that
55       is caught while blocked waiting to establish  a  connection,  connect()
56       shall  fail  and set errno to [EINTR], but the connection request shall
57       not be aborted, and the connection shall be established asynchronously.
58
59       If the connection cannot be established immediately and  O_NONBLOCK  is
60       set  for  the  file descriptor for the socket, connect() shall fail and
61       set errno to [EINPROGRESS], but the connection  request  shall  not  be
62       aborted, and the connection shall be established asynchronously. Subse‐
63       quent calls to connect() for the same socket, before the connection  is
64       established, shall fail and set errno to [EALREADY].
65
66       When  the  connection  has  been established asynchronously, pselect(),
67       select(), and poll() shall indicate that the file  descriptor  for  the
68       socket is ready for writing.
69
70       The  socket  in  use may require the process to have appropriate privi‐
71       leges to use the connect() function.
72

RETURN VALUE

74       Upon successful completion, connect() shall  return  0;  otherwise,  -1
75       shall be returned and errno set to indicate the error.
76

ERRORS

78       The connect() function shall fail if:
79
80       EADDRNOTAVAIL
81              The specified address is not available from the local machine.
82
83       EAFNOSUPPORT
84              The  specified  address  is  not a valid address for the address
85              family of the specified socket.
86
87       EALREADY
88              A connection request is already in progress  for  the  specified
89              socket.
90
91       EBADF  The socket argument is not a valid file descriptor.
92
93       ECONNREFUSED
94              The  target address was not listening for connections or refused
95              the connection request.
96
97       EINPROGRESS
98              O_NONBLOCK is set for the file descriptor for the socket and the
99              connection  cannot  be  immediately  established; the connection
100              shall be established asynchronously.
101
102       EINTR  The attempt to establish a connection was interrupted by  deliv‐
103              ery  of a signal that was caught; the connection shall be estab‐
104              lished asynchronously.
105
106       EISCONN
107              The specified socket is  connection-mode  and  is  already  con‐
108              nected.
109
110       ENETUNREACH
111              No route to the network is present.
112
113       ENOTSOCK
114              The socket argument does not refer to a socket.
115
116       EPROTOTYPE
117              The specified address has a different type than the socket bound
118              to the specified peer address.
119
120       ETIMEDOUT
121              The attempt to connect timed out before a connection was made.
122
123       If the address family of the socket is AF_UNIX,  then  connect()  shall
124       fail if:
125
126       EIO    An  I/O error occurred while reading from or writing to the file
127              system.
128
129       ELOOP  A loop exists in symbolic links encountered during resolution of
130              the pathname in address.
131
132       ENAMETOOLONG
133              The  length  of  a  component  of  a  pathname  is  longer  than
134              {NAME_MAX}.
135
136       ENOENT A component of the pathname does not name an  existing  file  or
137              the pathname is an empty string.
138
139       ENOTDIR
140              A  component of the path prefix of the pathname in address names
141              an existing file that is neither a directory nor a symbolic link
142              to a directory, or the pathname in address contains at least one
143              non-<slash> character and ends with one or more trailing <slash>
144              characters  and  the  last  pathname component names an existing
145              file that is neither a directory nor a symbolic link to a direc‐
146              tory.
147
148       The connect() function may fail if:
149
150       EACCES Search  permission is denied for a component of the path prefix;
151              or write access to the named socket is denied.
152
153       EADDRINUSE
154              Attempt to establish a connection that uses addresses  that  are
155              already in use.
156
157       ECONNRESET
158              Remote host reset the connection request.
159
160       EHOSTUNREACH
161              The  destination  host  cannot  be reached (probably because the
162              host is down or a remote router cannot reach it).
163
164       EINVAL The address_len argument is not a valid length for  the  address
165              family; or invalid address family in the sockaddr structure.
166
167       ELOOP  More  than  {SYMLOOP_MAX} symbolic links were encountered during
168              resolution of the pathname in address.
169
170       ENAMETOOLONG
171              The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
172              tion  of  a symbolic link produced an intermediate result with a
173              length that exceeds {PATH_MAX}.
174
175       ENETDOWN
176              The local network interface used to  reach  the  destination  is
177              down.
178
179       ENOBUFS
180              No buffer space is available.
181
182       EOPNOTSUPP
183              The socket is listening and cannot be connected.
184
185       The following sections are informative.
186

EXAMPLES

188       None.
189

APPLICATION USAGE

191       If  connect() fails, the state of the socket is unspecified. Conforming
192       applications should close the file descriptor and create a  new  socket
193       before attempting to reconnect.
194

RATIONALE

196       None.
197

FUTURE DIRECTIONS

199       None.
200

SEE ALSO

202       accept(),  bind(),  close(),  getsockname(), poll(), pselect(), send(),
203       shutdown(), socket()
204
205       The Base Definitions volume of POSIX.1‐2017, <sys_socket.h>
206
208       Portions of this text are reprinted and reproduced in  electronic  form
209       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
210       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
211       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
212       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
213       event of any discrepancy between this version and the original IEEE and
214       The Open Group Standard, the original IEEE and The Open Group  Standard
215       is  the  referee document. The original Standard can be obtained online
216       at http://www.opengroup.org/unix/online.html .
217
218       Any typographical or formatting errors that appear  in  this  page  are
219       most likely to have been introduced during the conversion of the source
220       files to man page format. To report such errors,  see  https://www.ker
221       nel.org/doc/man-pages/reporting_bugs.html .
222
223
224
225IEEE/The Open Group                  2017                          CONNECT(3P)
Impressum