OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / original / 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 2008-12-03 "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 *" 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 addr .
84 The
85 .I addrlen
86 argument specifies the size of
87 .IR addr .
88 The format of the address in
89 .I 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 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 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 AF_INET
159 see the description of
160 .I /proc/sys/net/ipv4/ip_local_port_range
161 .BR ip (7)
162 for information on how to increase the number of local ports.
163 .TP
164 .B EALREADY
165 The socket is nonblocking 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 nonblocking 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; see
204 .BR signal (7).
205 .\" For TCP, the connection will complete asynchronously.
206 .\" See http://lkml.org/lkml/2005/7/12/254
207 .TP
208 .B EISCONN
209 The socket is already connected.
210 .TP
211 .B ENETUNREACH
212 Network is unreachable.
213 .TP
214 .B ENOTSOCK
215 The file descriptor is not associated with a socket.
216 .TP
217 .B ETIMEDOUT
218 Timeout while attempting connection.
219 The server may be too
220 busy to accept new connections.
221 Note that for IP sockets the timeout may
222 be very long when syncookies are enabled on the server.
223 .SH "CONFORMING TO"
224 SVr4, 4.4BSD, (the
225 .BR connect ()
226 function first appeared in 4.2BSD), POSIX.1-2001.
227 .\" SVr4 documents the additional
228 .\" general error codes
229 .\" .BR EADDRNOTAVAIL ,
230 .\" .BR EINVAL ,
231 .\" .BR EAFNOSUPPORT ,
232 .\" .BR EALREADY ,
233 .\" .BR EINTR ,
234 .\" .BR EPROTOTYPE ,
235 .\" and
236 .\" .BR ENOSR .
237 .\" It also
238 .\" documents many additional error conditions not described here.
239 .SH NOTES
240 POSIX.1-2001 does not require the inclusion of
241 .IR <sys/types.h> ,
242 and this header file is not required on Linux.
243 However, some historical (BSD) implementations required this header
244 file, and portable applications are probably wise to include it.
245
246 The third argument of
247 .BR connect ()
248 is in reality an
249 .I int
250 (and this is what 4.x BSD and libc4 and libc5 have).
251 Some POSIX confusion resulted in the present
252 .IR socklen_t ,
253 also used by glibc.
254 See also
255 .BR accept (2).
256 .SH EXAMPLE
257 An example of the use of
258 .BR connect ()
259 is shown in
260 .BR getaddrinfo (3).
261 .SH "SEE ALSO"
262 .BR accept (2),
263 .BR bind (2),
264 .BR getsockname (2),
265 .BR listen (2),
266 .BR socket (2),
267 .BR path_resolution (7)