OSDN Git Service

(split) LDP: Update original to LDP v3.65
[linuxjm/LDP_man-pages.git] / original / man2 / connect.2
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 .\"
5 .\" %%%LICENSE_START(VERBATIM)
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.
25 .\" %%%LICENSE_END
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 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
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.
61 .\" %%%LICENSE_END
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 .\"
67 .TH CONNECT 2 2008-12-03 "Linux" "Linux Programmer's Manual"
68 .SH NAME
69 connect \- 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>
75 .sp
76 .BI "int connect(int " sockfd ", const struct sockaddr *" addr ,
77 .BI "            socklen_t " addrlen );
78 .fi
79 .SH DESCRIPTION
80 The
81 .BR connect ()
82 system call connects the socket referred to by the file descriptor
83 .I sockfd
84 to the address specified by
85 .IR addr .
86 The
87 .I addrlen
88 argument specifies the size of
89 .IR addr .
90 The format of the address in
91 .I addr
92 is determined by the address space of the socket
93 .IR sockfd ;
94 see
95 .BR socket (2)
96 for further details.
97
98 If the socket
99 .I sockfd
100 is of type
101 .BR SOCK_DGRAM ,
102 then
103 .I addr
104 is the address to which datagrams are sent by default, and the only
105 address from which datagrams are received.
106 If the socket is of type
107 .B SOCK_STREAM
108 or
109 .BR SOCK_SEQPACKET ,
110 this call attempts to make a connection to the socket that is bound
111 to the address specified by
112 .IR addr .
113 .PP
114 Generally, connection-based protocol sockets may successfully
115 .BR connect ()
116 only once; connectionless protocol sockets may use
117 .BR connect ()
118 multiple times to change their association.
119 Connectionless sockets may
120 dissolve the association by connecting to an address with the
121 .I sa_family
122 member of
123 .I sockaddr
124 set to
125 .BR AF_UNSPEC
126 (supported on Linux since kernel 2.2).
127 .SH RETURN VALUE
128 If the connection or binding succeeds, zero is returned.
129 On error, \-1 is returned, and
130 .I errno
131 is set appropriately.
132 .SH ERRORS
133 The following are general socket errors only.
134 There may be other domain-specific error codes.
135 .TP
136 .B EACCES
137 For UNIX domain sockets, which are identified by pathname:
138 Write permission is denied on the socket file,
139 or search permission is denied for one of the directories
140 in the path prefix.
141 (See also
142 .BR path_resolution (7).)
143 .TP
144 .BR EACCES ", " EPERM
145 The user tried to connect to a broadcast address without having the socket
146 broadcast flag enabled or the connection request failed because of a local
147 firewall rule.
148 .TP
149 .B EADDRINUSE
150 Local address is already in use.
151 .TP
152 .B EAFNOSUPPORT
153 The passed address didn't have the correct address family in its
154 .I sa_family
155 field.
156 .TP
157 .B EAGAIN
158 No more free local ports or insufficient entries in the routing cache.
159 For
160 .B AF_INET
161 see the description of
162 .I /proc/sys/net/ipv4/ip_local_port_range
163 .BR ip (7)
164 for information on how to increase the number of local ports.
165 .TP
166 .B EALREADY
167 The socket is nonblocking and a previous connection attempt has not yet
168 been completed.
169 .TP
170 .B EBADF
171 The file descriptor is not a valid index in the descriptor table.
172 .TP
173 .B ECONNREFUSED
174 No-one listening on the remote address.
175 .TP
176 .B EFAULT
177 The socket structure address is outside the user's address space.
178 .TP
179 .B EINPROGRESS
180 The socket is nonblocking and the connection cannot be completed
181 immediately.
182 It is possible to
183 .BR select (2)
184 or
185 .BR poll (2)
186 for completion by selecting the socket for writing.
187 After
188 .BR select (2)
189 indicates writability, use
190 .BR getsockopt (2)
191 to read the
192 .B SO_ERROR
193 option at level
194 .B SOL_SOCKET
195 to determine whether
196 .BR connect ()
197 completed successfully
198 .RB ( SO_ERROR
199 is zero) or unsuccessfully
200 .RB ( SO_ERROR
201 is one of the usual error codes listed here,
202 explaining the reason for the failure).
203 .TP
204 .B EINTR
205 The system call was interrupted by a signal that was caught; see
206 .BR signal (7).
207 .\" For TCP, the connection will complete asynchronously.
208 .\" See http://lkml.org/lkml/2005/7/12/254
209 .TP
210 .B EISCONN
211 The socket is already connected.
212 .TP
213 .B ENETUNREACH
214 Network is unreachable.
215 .TP
216 .B ENOTSOCK
217 The file descriptor is not associated with a socket.
218 .TP
219 .B ETIMEDOUT
220 Timeout while attempting connection.
221 The server may be too
222 busy to accept new connections.
223 Note that for IP sockets the timeout may
224 be very long when syncookies are enabled on the server.
225 .SH CONFORMING TO
226 SVr4, 4.4BSD, (the
227 .BR connect ()
228 function first appeared in 4.2BSD), POSIX.1-2001.
229 .\" SVr4 documents the additional
230 .\" general error codes
231 .\" .BR EADDRNOTAVAIL ,
232 .\" .BR EINVAL ,
233 .\" .BR EAFNOSUPPORT ,
234 .\" .BR EALREADY ,
235 .\" .BR EINTR ,
236 .\" .BR EPROTOTYPE ,
237 .\" and
238 .\" .BR ENOSR .
239 .\" It also
240 .\" documents many additional error conditions not described here.
241 .SH NOTES
242 POSIX.1-2001 does not require the inclusion of
243 .IR <sys/types.h> ,
244 and this header file is not required on Linux.
245 However, some historical (BSD) implementations required this header
246 file, and portable applications are probably wise to include it.
247
248 The third argument of
249 .BR connect ()
250 is in reality an
251 .I int
252 (and this is what 4.x BSD and libc4 and libc5 have).
253 Some POSIX confusion resulted in the present
254 .IR socklen_t ,
255 also used by glibc.
256 See also
257 .BR accept (2).
258 .SH EXAMPLE
259 An example of the use of
260 .BR connect ()
261 is shown in
262 .BR getaddrinfo (3).
263 .SH SEE ALSO
264 .BR accept (2),
265 .BR bind (2),
266 .BR getsockname (2),
267 .BR listen (2),
268 .BR socket (2),
269 .BR path_resolution (7)
270 .SH COLOPHON
271 This page is part of release 3.65 of the Linux
272 .I man-pages
273 project.
274 A description of the project,
275 and information about reporting bugs,
276 can be found at
277 \%http://www.kernel.org/doc/man\-pages/.