]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/connect.2
mknod.2: tfix
[thirdparty/man-pages.git] / man2 / connect.2
CommitLineData
77117f4f
MK
1.\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2.\" Portions extracted from /usr/include/sys/socket.h, which does not have
3.\" any authorship information in it. It is probably available under the GPL.
4.\"
93015253 5.\" %%%LICENSE_START(VERBATIM)
77117f4f
MK
6.\" Permission is granted to make and distribute verbatim copies of this
7.\" manual provided the copyright notice and this permission notice are
8.\" preserved on all copies.
9.\"
10.\" Permission is granted to copy and distribute modified versions of this
11.\" manual under the conditions for verbatim copying, provided that the
12.\" entire resulting derived work is distributed under the terms of a
13.\" permission notice identical to this one.
14.\"
15.\" Since the Linux kernel and libraries are constantly changing, this
16.\" manual page may be incorrect or out-of-date. The author(s) assume no
17.\" responsibility for errors or omissions, or for damages resulting from
18.\" the use of the information contained herein. The author(s) may not
19.\" have taken the same level of care in the production of this manual,
20.\" which is licensed free of charge, as they might when working
21.\" professionally.
22.\"
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 25.\" %%%LICENSE_END
77117f4f
MK
26.\"
27.\"
28.\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
29.\"
30.\" Copyright (c) 1983 The Regents of the University of California.
31.\" All rights reserved.
32.\"
a9cd9cb7 33.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
77117f4f
MK
34.\" Redistribution and use in source and binary forms, with or without
35.\" modification, are permitted provided that the following conditions
36.\" are met:
37.\" 1. Redistributions of source code must retain the above copyright
38.\" notice, this list of conditions and the following disclaimer.
39.\" 2. Redistributions in binary form must reproduce the above copyright
40.\" notice, this list of conditions and the following disclaimer in the
41.\" documentation and/or other materials provided with the distribution.
42.\" 3. All advertising materials mentioning features or use of this software
43.\" must display the following acknowledgement:
44.\" This product includes software developed by the University of
45.\" California, Berkeley and its contributors.
46.\" 4. Neither the name of the University nor the names of its contributors
47.\" may be used to endorse or promote products derived from this software
48.\" without specific prior written permission.
49.\"
50.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60.\" SUCH DAMAGE.
8c9302dc 61.\" %%%LICENSE_END
77117f4f
MK
62.\"
63.\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
64.\" Modified 1998, 1999 by Andi Kleen
65.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
66.\"
e8426ca2 67.TH CONNECT 2 2020-04-11 "Linux" "Linux Programmer's Manual"
77117f4f
MK
68.SH NAME
69connect \- initiate a connection on a socket
70.SH SYNOPSIS
71.nf
72.BR "#include <sys/types.h>" " /* See NOTES */"
73.br
74.B #include <sys/socket.h>
68e4db0a 75.PP
a2b8b6d6 76.BI "int connect(int " sockfd ", const struct sockaddr *" addr ,
77117f4f
MK
77.BI " socklen_t " addrlen );
78.fi
79.SH DESCRIPTION
80The
81.BR connect ()
82system call connects the socket referred to by the file descriptor
83.I sockfd
84to the address specified by
a2b8b6d6 85.IR addr .
77117f4f
MK
86The
87.I addrlen
88argument specifies the size of
a2b8b6d6 89.IR addr .
77117f4f 90The format of the address in
a2b8b6d6 91.I addr
77117f4f
MK
92is determined by the address space of the socket
93.IR sockfd ;
94see
95.BR socket (2)
96for further details.
efeece04 97.PP
77117f4f
MK
98If the socket
99.I sockfd
100is of type
45d522a8 101.BR SOCK_DGRAM ,
77117f4f 102then
a2b8b6d6 103.I addr
77117f4f
MK
104is the address to which datagrams are sent by default, and the only
105address from which datagrams are received.
106If the socket is of type
107.B SOCK_STREAM
108or
109.BR SOCK_SEQPACKET ,
110this call attempts to make a connection to the socket that is bound
111to the address specified by
a2b8b6d6 112.IR addr .
77117f4f 113.PP
f4ba6a21
MK
114Some protocol sockets (e.g., UNIX domain stream sockets)
115may successfully
77117f4f 116.BR connect ()
f4ba6a21
MK
117only once.
118.PP
119Some protocol sockets
120(e.g., datagram sockets in the UNIX and Internet domains)
121may use
77117f4f
MK
122.BR connect ()
123multiple times to change their association.
f4ba6a21
MK
124.PP
125Some protocol sockets
126(e.g., TCP sockets as well as datagram sockets in the UNIX and
127Internet domains)
128may dissolve the association by connecting to an address with the
77117f4f
MK
129.I sa_family
130member of
131.I sockaddr
132set to
f4ba6a21
MK
133.BR AF_UNSPEC;
134thereafter, the socket can be connected to another address.
135.RB ( AF_UNSPEC
136is supported on Linux since kernel 2.2.)
47297adb 137.SH RETURN VALUE
77117f4f
MK
138If the connection or binding succeeds, zero is returned.
139On error, \-1 is returned, and
140.I errno
141is set appropriately.
142.SH ERRORS
143The following are general socket errors only.
144There may be other domain-specific error codes.
145.TP
146.B EACCES
008f1ecc 147For UNIX domain sockets, which are identified by pathname:
77117f4f
MK
148Write permission is denied on the socket file,
149or search permission is denied for one of the directories
150in the path prefix.
151(See also
152.BR path_resolution (7).)
153.TP
154.BR EACCES ", " EPERM
155The user tried to connect to a broadcast address without having the socket
156broadcast flag enabled or the connection request failed because of a local
157firewall rule.
158.TP
159.B EADDRINUSE
160Local address is already in use.
161.TP
ac7477eb
MK
162.B EADDRNOTAVAIL
163(Internet domain sockets)
164The socket referred to by
165.I sockfd
166had not previously been bound to an address and,
167upon attempting to bind it to an ephemeral port,
168it was determined that all port numbers in the ephemeral port range
169are currently in use.
170See the discussion of
171.I /proc/sys/net/ipv4/ip_local_port_range
172in
173.BR ip (7).
174.TP
77117f4f
MK
175.B EAFNOSUPPORT
176The passed address didn't have the correct address family in its
177.I sa_family
178field.
179.TP
180.B EAGAIN
14662577 181For nonblocking UNIX domain sockets, the socket is nonblocking, and the
55dc41dc
MK
182connection cannot be completed immediately.
183For other socket families, there are insufficient entries in the routing cache.
77117f4f
MK
184.TP
185.B EALREADY
ff40dbb3 186The socket is nonblocking and a previous connection attempt has not yet
77117f4f
MK
187been completed.
188.TP
189.B EBADF
d60d564d
MK
190.I sockfd
191is not a valid open file descriptor.
77117f4f
MK
192.TP
193.B ECONNREFUSED
96a35a83
MK
194A
195.BR connect ()
196on a stream socket found no one listening on the remote address.
77117f4f
MK
197.TP
198.B EFAULT
199The socket structure address is outside the user's address space.
200.TP
201.B EINPROGRESS
55dc41dc 202The socket is nonblocking and the connection cannot be completed immediately.
85bb10df 203(UNIX domain sockets failed with
14662577
BP
204.BR EAGAIN
205instead.)
77117f4f
MK
206It is possible to
207.BR select (2)
208or
209.BR poll (2)
210for completion by selecting the socket for writing.
211After
212.BR select (2)
213indicates writability, use
214.BR getsockopt (2)
215to read the
216.B SO_ERROR
217option at level
218.B SOL_SOCKET
219to determine whether
220.BR connect ()
221completed successfully
222.RB ( SO_ERROR
223is zero) or unsuccessfully
224.RB ( SO_ERROR
225is one of the usual error codes listed here,
226explaining the reason for the failure).
227.TP
228.B EINTR
229The system call was interrupted by a signal that was caught; see
230.BR signal (7).
231.\" For TCP, the connection will complete asynchronously.
232.\" See http://lkml.org/lkml/2005/7/12/254
233.TP
234.B EISCONN
235The socket is already connected.
236.TP
237.B ENETUNREACH
238Network is unreachable.
239.TP
240.B ENOTSOCK
deedfd97
MK
241The file descriptor
242.I sockfd
243does not refer to a socket.
77117f4f 244.TP
65558bed
MK
245.B EPROTOTYPE
246The socket type does not support the requested communications protocol.
247This error can occur, for example,
248on an attempt to connect a UNIX domain datagram socket to a stream socket.
249.TP
77117f4f
MK
250.B ETIMEDOUT
251Timeout while attempting connection.
252The server may be too
253busy to accept new connections.
254Note that for IP sockets the timeout may
255be very long when syncookies are enabled on the server.
47297adb 256.SH CONFORMING TO
d5bc35a7
MK
257POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD,
258.RB (connect ()
259first appeared in 4.2BSD).
77117f4f
MK
260.\" SVr4 documents the additional
261.\" general error codes
262.\" .BR EADDRNOTAVAIL ,
263.\" .BR EINVAL ,
264.\" .BR EAFNOSUPPORT ,
265.\" .BR EALREADY ,
266.\" .BR EINTR ,
267.\" .BR EPROTOTYPE ,
268.\" and
269.\" .BR ENOSR .
270.\" It also
271.\" documents many additional error conditions not described here.
272.SH NOTES
d1969970 273POSIX.1 does not require the inclusion of
77117f4f
MK
274.IR <sys/types.h> ,
275and this header file is not required on Linux.
276However, some historical (BSD) implementations required this header
277file, and portable applications are probably wise to include it.
efeece04 278.PP
ec5df7af
MK
279For background on the
280.I socklen_t
281type, see
77117f4f 282.BR accept (2).
efeece04 283.PP
977e3384 284If
536f641b 285.BR connect ()
977e3384
MH
286fails, consider the state of the socket as unspecified.
287Portable applications should close the socket and create a new one for
288reconnecting.
77117f4f
MK
289.SH EXAMPLE
290An example of the use of
291.BR connect ()
292is shown in
293.BR getaddrinfo (3).
47297adb 294.SH SEE ALSO
77117f4f
MK
295.BR accept (2),
296.BR bind (2),
297.BR getsockname (2),
298.BR listen (2),
299.BR socket (2),
300.BR path_resolution (7)