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
12 connect - connect a socket
13
15 #include <sys/socket.h>
16
17 int connect(int socket, const struct sockaddr *address,
18 socklen_t address_len);
19
20
22 The connect() function shall attempt to make a connection on a socket.
23 The function takes the following arguments:
24
25 socket Specifies the file descriptor associated with the socket.
26
27 address
28 Points to a sockaddr structure containing the peer address. The
29 length and format of the address depend on the address family of
30 the socket.
31
32 address_len
33 Specifies the length of the sockaddr structure pointed to by the
34 address argument.
35
36
37 If the socket has not already been bound to a local address, connect()
38 shall bind it to an address which, unless the socket's address family
39 is AF_UNIX, is an unused local address.
40
41 If the initiating socket is not connection-mode, then connect() shall
42 set the socket's peer address, and no connection is made. For
43 SOCK_DGRAM sockets, the peer address identifies where all datagrams are
44 sent on subsequent send() functions, and limits the remote sender for
45 subsequent recv() functions. If address is a null address for the pro‐
46 tocol, the socket's peer address shall be reset.
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, select() and
68 poll() shall indicate that the file descriptor for the socket is ready
69 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
125 If the address family of the socket is AF_UNIX, then connect() shall
126 fail if:
127
128 EIO An I/O error occurred while reading from or writing to the file
129 system.
130
131 ELOOP A loop exists in symbolic links encountered during resolution of
132 the pathname in address.
133
134 ENAMETOOLONG
135 A component of a pathname exceeded {NAME_MAX} characters, or an
136 entire pathname exceeded {PATH_MAX} characters.
137
138 ENOENT A component of the pathname does not name an existing file or
139 the pathname is an empty string.
140
141 ENOTDIR
142 A component of the path prefix of the pathname in address is not
143 a directory.
144
145
146 The connect() function may fail if:
147
148 EACCES Search permission is denied for a component of the path prefix;
149 or write access to the named socket is denied.
150
151 EADDRINUSE
152 Attempt to establish a connection that uses addresses that are
153 already in use.
154
155 ECONNRESET
156 Remote host reset the connection request.
157
158 EHOSTUNREACH
159 The destination host cannot be reached (probably because the
160 host is down or a remote router cannot reach it).
161
162 EINVAL The address_len argument is not a valid length for the address
163 family; or invalid address family in the sockaddr structure.
164
165 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
166 resolution of the pathname in address.
167
168 ENAMETOOLONG
169 Pathname resolution of a symbolic link produced an intermediate
170 result whose length exceeds {PATH_MAX}.
171
172 ENETDOWN
173 The local network interface used to reach the destination is
174 down.
175
176 ENOBUFS
177 No buffer space is available.
178
179 EOPNOTSUPP
180 The socket is listening and cannot be connected.
181
182
183 The following sections are informative.
184
186 None.
187
189 If connect() fails, the state of the socket is unspecified. Conforming
190 applications should close the file descriptor and create a new socket
191 before attempting to reconnect.
192
194 None.
195
197 None.
198
200 accept(), bind(), close(), getsockname(), poll(), select(), send(),
201 shutdown(), socket(), the Base Definitions volume of
202 IEEE Std 1003.1-2001, <sys/socket.h>
203
205 Portions of this text are reprinted and reproduced in electronic form
206 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
207 -- Portable Operating System Interface (POSIX), The Open Group Base
208 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
209 Electrical and Electronics Engineers, Inc and The Open Group. In the
210 event of any discrepancy between this version and the original IEEE and
211 The Open Group Standard, the original IEEE and The Open Group Standard
212 is the referee document. The original Standard can be obtained online
213 at http://www.opengroup.org/unix/online.html .
214
215
216
217IEEE/The Open Group 2003 CONNECT(3P)