]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/connect.2
Hyphenation fixes
[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 2004-06-23 "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 .SH "RETURN VALUE"
125 If the connection or binding succeeds, zero is returned.
126 On error, \-1 is returned, and
127 .I errno
128 is set appropriately.
129 .SH ERRORS
130 The following are general socket errors only.
131 There may be other domain-specific error codes.
132 .TP
133 .B EACCES
134 For Unix domain sockets, which are identified by pathname:
135 Write permission is denied on the socket file,
136 or search permission is denied for one of the directories
137 in the path prefix.
138 (See also
139 .BR path_resolution (7).)
140 .TP
141 .B EACCES, EPERM
142 The user tried to connect to a broadcast address without having the socket
143 broadcast flag enabled or the connection request failed because of a local
144 firewall rule.
145 .TP
146 .B EADDRINUSE
147 Local address is already in use.
148 .TP
149 .B EAFNOSUPPORT
150 The passed address didn't have the correct address family in its
151 .I sa_family
152 field.
153 .TP
154 .B EAGAIN
155 No more free local ports or insufficient entries in the routing cache.
156 For
157 .B PF_INET
158 see the
159 .I net.ipv4.ip_local_port_range
160 sysctl in
161 .BR ip (7)
162 on how to increase the number of local ports.
163 .TP
164 .B EALREADY
165 The socket is non-blocking and a previous connection attempt has not yet
166 been completed.
167 .TP
168 .B EBADF
169 The file descriptor is not a valid index in the descriptor table.
170 .TP
171 .B ECONNREFUSED
172 No-one listening on the remote address.
173 .TP
174 .B EFAULT
175 The socket structure address is outside the user's address space.
176 .TP
177 .B EINPROGRESS
178 The socket is non-blocking and the connection cannot be completed
179 immediately.
180 It is possible to
181 .BR select (2)
182 or
183 .BR poll (2)
184 for completion by selecting the socket for writing.
185 After
186 .BR select (2)
187 indicates writability, use
188 .BR getsockopt (2)
189 to read the
190 .B SO_ERROR
191 option at level
192 .B SOL_SOCKET
193 to determine whether
194 .BR connect ()
195 completed successfully
196 .RB ( SO_ERROR
197 is zero) or unsuccessfully
198 .RB ( SO_ERROR
199 is one of the usual error codes listed here,
200 explaining the reason for the failure).
201 .TP
202 .B EINTR
203 The system call was interrupted by a signal that was caught.
204 .\" For TCP, the connection will complete asynchronously.
205 .\" See http://lkml.org/lkml/2005/7/12/254
206 .TP
207 .B EISCONN
208 The socket is already connected.
209 .TP
210 .B ENETUNREACH
211 Network is unreachable.
212 .TP
213 .B ENOTSOCK
214 The file descriptor is not associated with a socket.
215 .TP
216 .B ETIMEDOUT
217 Timeout while attempting connection.
218 The server may be too
219 busy to accept new connections.
220 Note that for IP sockets the timeout may
221 be very long when syncookies are enabled on the server.
222 .SH "CONFORMING TO"
223 SVr4, 4.4BSD, (the
224 .BR connect ()
225 function first appeared in 4.2BSD), POSIX.1-2001.
226 .\" SVr4 documents the additional
227 .\" general error codes
228 .\" .BR EADDRNOTAVAIL ,
229 .\" .BR EINVAL ,
230 .\" .BR EAFNOSUPPORT ,
231 .\" .BR EALREADY ,
232 .\" .BR EINTR ,
233 .\" .BR EPROTOTYPE ,
234 .\" and
235 .\" .BR ENOSR .
236 .\" It also
237 .\" documents many additional error conditions not described here.
238 .SH NOTES
239 POSIX.1-2001 does not require the inclusion of
240 .IR <sys/types.h> ,
241 and this header file is not required on Linux.
242 However, some historical (BSD) implementations required this header
243 file, and portable applications are probably wise to include it.
244
245 The third argument of
246 .BR connect ()
247 is in reality an
248 .I int
249 (and this is what 4.x BSD and libc4 and libc5 have).
250 Some POSIX confusion resulted in the present
251 .IR socklen_t ,
252 also used by glibc.
253 See also
254 .BR accept (2).
255 .SH BUGS
256 Unconnecting a socket by calling
257 .BR connect ()
258 with a
259 .B AF_UNSPEC
260 address is not yet implemented.
261 .SH EXAMPLE
262 An example of the use of
263 .BR connect ()
264 is shown in
265 .BR getaddrinfo (3).
266 .SH "SEE ALSO"
267 .BR accept (2),
268 .BR bind (2),
269 .BR getsockname (2),
270 .BR listen (2),
271 .BR socket (2),
272 .BR path_resolution (7)