1CONNECT(3P) POSIX Programmer's Manual CONNECT(3P)
2
3
4
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
11
13 connect — connect a socket
14
16 #include <sys/socket.h>
17
18 int connect(int socket, const struct sockaddr *address,
19 socklen_t address_len);
20
22 The connect() function shall attempt to make a connection on a connec‐
23 tion-mode socket or to set or reset the peer address of a connection‐
24 less-mode socket. The function takes the following arguments:
25
26 socket Specifies the file descriptor associated with the socket.
27
28 address Points to a sockaddr structure containing the peer address.
29 The length and format of the address depend on the address
30 family of the socket.
31
32 address_len Specifies the length of the sockaddr structure pointed to
33 by the address argument.
34
35 If the socket has not already been bound to a local address, connect()
36 shall bind it to an address which, unless the socket's address family
37 is AF_UNIX, is an unused local address.
38
39 If the initiating socket is not connection-mode, then connect() shall
40 set the socket's peer address, and no connection is made. For
41 SOCK_DGRAM sockets, the peer address identifies where all datagrams are
42 sent on subsequent send() functions, and limits the remote sender for
43 subsequent recv() functions. If the sa_family member of address is
44 AF_UNSPEC, the socket's peer address shall be reset. Note that despite
45 no connection being made, the term ``connected'' is used to describe a
46 connectionless-mode socket for which a peer address has been set.
47
48 If the initiating socket is connection-mode, then connect() shall
49 attempt to establish a connection to the address specified by the
50 address argument. If the connection cannot be established immediately
51 and O_NONBLOCK is not set for the file descriptor for the socket, con‐
52 nect() shall block for up to an unspecified timeout interval until the
53 connection is established. If the timeout interval expires before the
54 connection is established, connect() shall fail and the connection
55 attempt shall be aborted. If connect() is interrupted by a signal that
56 is caught while blocked waiting to establish a connection, connect()
57 shall fail and set errno to [EINTR], but the connection request shall
58 not be aborted, and the connection shall be established asynchronously.
59
60 If the connection cannot be established immediately and O_NONBLOCK is
61 set for the file descriptor for the socket, connect() shall fail and
62 set errno to [EINPROGRESS], but the connection request shall not be
63 aborted, and the connection shall be established asynchronously. Subse‐
64 quent calls to connect() for the same socket, before the connection is
65 established, shall fail and set errno to [EALREADY].
66
67 When the connection has been established asynchronously, pselect(),
68 select(), and poll() shall indicate that the file descriptor for the
69 socket is ready for writing.
70
71 The socket in use may require the process to have appropriate privi‐
72 leges to use the connect() function.
73
75 Upon successful completion, connect() shall return 0; otherwise, −1
76 shall be returned and errno set to indicate the error.
77
79 The connect() function shall fail if:
80
81 EADDRNOTAVAIL
82 The specified address is not available from the local machine.
83
84 EAFNOSUPPORT
85 The specified address is not a valid address for the address
86 family of the specified socket.
87
88 EALREADY
89 A connection request is already in progress for the specified
90 socket.
91
92 EBADF The socket argument is not a valid file descriptor.
93
94 ECONNREFUSED
95 The target address was not listening for connections or refused
96 the connection request.
97
98 EINPROGRESS
99 O_NONBLOCK is set for the file descriptor for the socket and the
100 connection cannot be immediately established; the connection
101 shall be established asynchronously.
102
103 EINTR The attempt to establish a connection was interrupted by deliv‐
104 ery of a signal that was caught; the connection shall be estab‐
105 lished asynchronously.
106
107 EISCONN
108 The specified socket is connection-mode and is already con‐
109 nected.
110
111 ENETUNREACH
112 No route to the network is present.
113
114 ENOTSOCK
115 The socket argument does not refer to a socket.
116
117 EPROTOTYPE
118 The specified address has a different type than the socket bound
119 to the specified peer address.
120
121 ETIMEDOUT
122 The attempt to connect timed out before a connection was made.
123
124 If the address family of the socket is AF_UNIX, then connect() shall
125 fail if:
126
127 EIO An I/O error occurred while reading from or writing to the file
128 system.
129
130 ELOOP A loop exists in symbolic links encountered during resolution of
131 the pathname in address.
132
133 ENAMETOOLONG
134 The length of a component of a pathname is longer than
135 {NAME_MAX}.
136
137 ENOENT A component of the pathname does not name an existing file or
138 the pathname is an empty string.
139
140 ENOTDIR
141 A component of the path prefix of the pathname in address names
142 an existing file that is neither a directory nor a symbolic link
143 to a directory, or the pathname in address contains at least one
144 non-<slash> character and ends with one or more trailing <slash>
145 characters and the last pathname component names an existing
146 file that is neither a directory nor a symbolic link to a direc‐
147 tory.
148
149 The connect() function may fail if:
150
151 EACCES Search permission is denied for a component of the path prefix;
152 or write access to the named socket is denied.
153
154 EADDRINUSE
155 Attempt to establish a connection that uses addresses that are
156 already in use.
157
158 ECONNRESET
159 Remote host reset the connection request.
160
161 EHOSTUNREACH
162 The destination host cannot be reached (probably because the
163 host is down or a remote router cannot reach it).
164
165 EINVAL The address_len argument is not a valid length for the address
166 family; or invalid address family in the sockaddr structure.
167
168 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
169 resolution of the pathname in address.
170
171 ENAMETOOLONG
172 The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
173 tion of a symbolic link produced an intermediate result with a
174 length that exceeds {PATH_MAX}.
175
176 ENETDOWN
177 The local network interface used to reach the destination is
178 down.
179
180 ENOBUFS
181 No buffer space is available.
182
183 EOPNOTSUPP
184 The socket is listening and cannot be connected.
185
186 The following sections are informative.
187
189 None.
190
192 If connect() fails, the state of the socket is unspecified. Conforming
193 applications should close the file descriptor and create a new socket
194 before attempting to reconnect.
195
197 None.
198
200 None.
201
203 accept(), bind(), close(), getsockname(), poll(), pselect(), send(),
204 shutdown(), socket()
205
206 The Base Definitions volume of POSIX.1‐2008, <sys_socket.h>
207
209 Portions of this text are reprinted and reproduced in electronic form
210 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
211 -- Portable Operating System Interface (POSIX), The Open Group Base
212 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
213 cal and Electronics Engineers, Inc and The Open Group. (This is
214 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
215 event of any discrepancy between this version and the original IEEE and
216 The Open Group Standard, the original IEEE and The Open Group Standard
217 is the referee document. The original Standard can be obtained online
218 at http://www.unix.org/online.html .
219
220 Any typographical or formatting errors that appear in this page are
221 most likely to have been introduced during the conversion of the source
222 files to man page format. To report such errors, see https://www.ker‐
223 nel.org/doc/man-pages/reporting_bugs.html .
224
225
226
227IEEE/The Open Group 2013 CONNECT(3P)