summaryrefslogtreecommitdiffstats
path: root/man-pages-posix-2017/man3p/connect.3p
blob: bd2232c27c67366caecbc7005e1a8e5283d27201 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
'\" et
.TH CONNECT "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.\"
.SH NAME
connect
\(em connect a socket
.SH SYNOPSIS
.LP
.nf
#include <sys/socket.h>
.P
int connect(int \fIsocket\fP, const struct sockaddr *\fIaddress\fP,
    socklen_t \fIaddress_len\fP);
.fi
.SH DESCRIPTION
The
\fIconnect\fR()
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:
.IP "\fIsocket\fR" 12
Specifies the file descriptor associated with the socket.
.IP "\fIaddress\fR" 12
Points to a
.BR sockaddr
structure containing the peer address. The length and format of the
address depend on the address family of the socket.
.IP "\fIaddress_len\fR" 12
Specifies the length of the
.BR sockaddr
structure pointed to by the
.IR address
argument.
.P
If the socket has not already been bound to a local address,
\fIconnect\fR()
shall bind it to an address which, unless the socket's address family
is AF_UNIX, is an unused local address.
.P
If the initiating socket is not connection-mode, then
\fIconnect\fR()
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
\fIsend\fR()
functions, and limits the remote sender for subsequent
\fIrecv\fR()
functions. If the
.IR sa_family
member of
.IR address
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.
.P
If the initiating socket is connection-mode, then
\fIconnect\fR()
shall attempt to establish a connection to the address specified by the
.IR address
argument. If the connection cannot be established immediately and
O_NONBLOCK is not set for the file descriptor for the socket,
\fIconnect\fR()
shall block for up to an unspecified timeout interval until the
connection is established. If the timeout interval expires before the
connection is established,
\fIconnect\fR()
shall fail and the connection attempt shall be aborted. If
\fIconnect\fR()
is interrupted by a signal that is caught while blocked waiting to
establish a connection,
\fIconnect\fR()
shall fail and set
.IR errno
to
.BR [EINTR] ,
but the connection request shall not be aborted, and the connection
shall be established asynchronously.
.P
If the connection cannot be established immediately and O_NONBLOCK is
set for the file descriptor for the socket,
\fIconnect\fR()
shall fail and set
.IR errno
to
.BR [EINPROGRESS] ,
but the connection request shall not be aborted, and the connection
shall be established asynchronously. Subsequent calls to
\fIconnect\fR()
for the same socket, before the connection is established, shall fail
and set
.IR errno
to
.BR [EALREADY] .
.P
When the connection has been established asynchronously,
\fIpselect\fR(),
\fIselect\fR(),
and
\fIpoll\fR()
shall indicate that the file descriptor for the socket is ready for
writing.
.P
The socket in use may require the process to have appropriate
privileges to use the
\fIconnect\fR()
function.
.SH "RETURN VALUE"
Upon successful completion,
\fIconnect\fR()
shall return 0; otherwise, \-1 shall be returned and
.IR errno
set to indicate the error.
.SH ERRORS
The
\fIconnect\fR()
function shall fail if:
.TP
.BR EADDRNOTAVAIL
.br
The specified address is not available from the local machine.
.TP
.BR EAFNOSUPPORT
.br
The specified address is not a valid address for the address family of
the specified socket.
.TP
.BR EALREADY
A connection request is already in progress for the specified socket.
.TP
.BR EBADF
The
.IR socket
argument is not a valid file descriptor.
.TP
.BR ECONNREFUSED
.br
The target address was not listening for connections or refused the
connection request.
.TP
.BR EINPROGRESS
O_NONBLOCK is set for the file descriptor for the socket and the
connection cannot be immediately established; the connection shall be
established asynchronously.
.TP
.BR EINTR
The attempt to establish a connection was interrupted by delivery of a
signal that was caught; the connection shall be established
asynchronously.
.TP
.BR EISCONN
The specified socket is connection-mode and is already connected.
.TP
.BR ENETUNREACH
.br
No route to the network is present.
.TP
.BR ENOTSOCK
The
.IR socket
argument does not refer to a socket.
.TP
.BR EPROTOTYPE
The specified address has a different type than the socket bound to the
specified peer address.
.TP
.BR ETIMEDOUT
The attempt to connect timed out before a connection was made.
.P
If the address family of the socket is AF_UNIX, then
\fIconnect\fR()
shall fail if:
.TP
.BR EIO
An I/O error occurred while reading from or writing to the file system.
.TP
.BR ELOOP
A loop exists in symbolic links encountered during resolution of the
pathname in
.IR address .
.TP
.BR ENAMETOOLONG
.br
The length of a component of a pathname is longer than
{NAME_MAX}.
.TP
.BR ENOENT
A component of the pathname does not name an existing file or the
pathname is an empty string.
.TP
.BR ENOTDIR
A component of the path prefix of the pathname in
.IR address
names an existing file that is neither a directory nor a symbolic link
to a directory, or the pathname in
.IR address
contains at least one non-\c
<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.
.P
The
\fIconnect\fR()
function may fail if:
.TP
.BR EACCES
Search permission is denied for a component of the path prefix; or
write access to the named socket is denied.
.TP
.BR EADDRINUSE
Attempt to establish a connection that uses addresses that are already
in use.
.TP
.BR ECONNRESET
Remote host reset the connection request.
.TP
.BR EHOSTUNREACH
.br
The destination host cannot be reached (probably because the host is
down or a remote router cannot reach it).
.TP
.BR EINVAL
The
.IR address_len
argument is not a valid length for the address family; or invalid
address family in the
.BR sockaddr
structure.
.TP
.BR ELOOP
More than
{SYMLOOP_MAX}
symbolic links were encountered during resolution of the pathname in
.IR address .
.TP
.BR ENAMETOOLONG
.br
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}.
.TP
.BR ENETDOWN
The local network interface used to reach the destination is down.
.TP
.BR ENOBUFS
No buffer space is available.
.TP
.BR EOPNOTSUPP
The socket is listening and cannot be connected.
.LP
.IR "The following sections are informative."
.SH EXAMPLES
None.
.SH "APPLICATION USAGE"
If
\fIconnect\fR()
fails, the state of the socket is unspecified. Conforming applications
should close the file descriptor and create a new socket before
attempting to reconnect.
.SH "RATIONALE"
None.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "\fIaccept\fR\^(\|)",
.IR "\fIbind\fR\^(\|)",
.IR "\fIclose\fR\^(\|)",
.IR "\fIgetsockname\fR\^(\|)",
.IR "\fIpoll\fR\^(\|)",
.IR "\fIpselect\fR\^(\|)",
.IR "\fIsend\fR\^(\|)",
.IR "\fIshutdown\fR\^(\|)",
.IR "\fIsocket\fR\^(\|)"
.P
The Base Definitions volume of POSIX.1\(hy2017,
.IR "\fB<sys_socket.h>\fP"
.\"
.SH COPYRIGHT
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 .
.PP
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 .