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