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.
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.
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.
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
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
30 .\" Copyright (c) 1983 The Regents of the University of California.
31 .\" All rights reserved.
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
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.
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
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>
67 .TH CONNECT 2 2017-09-15 "Linux" "Linux Programmer's Manual"
69 connect \- initiate a connection on a socket
72 .BR "#include <sys/types.h>" " /* See NOTES */"
74 .B #include <sys/socket.h>
76 .BI "int connect(int " sockfd ", const struct sockaddr *" addr ,
77 .BI " socklen_t " addrlen );
82 system call connects the socket referred to by the file descriptor
84 to the address specified by
88 argument specifies the size of
90 The format of the address in
92 is determined by the address space of the socket
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
110 this call attempts to make a connection to the socket that is bound
111 to the address specified by
114 Generally, connection-based protocol sockets may successfully
116 only once; connectionless protocol sockets may use
118 multiple times to change their association.
119 Connectionless sockets may
120 dissolve the association by connecting to an address with the
126 (supported on Linux since kernel 2.2).
128 If the connection or binding succeeds, zero is returned.
129 On error, \-1 is returned, and
131 is set appropriately.
133 The following are general socket errors only.
134 There may be other domain-specific error codes.
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
142 .BR path_resolution (7).)
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
150 Local address is already in use.
153 (Internet domain sockets)
154 The socket referred to by
156 had not previously been bound to an address and,
157 upon attempting to bind it to an ephemeral port,
158 it was determined that all port numbers in the ephemeral port range
159 are currently in use.
160 See the discussion of
161 .I /proc/sys/net/ipv4/ip_local_port_range
166 The passed address didn't have the correct address family in its
171 Insufficient entries in the routing cache.
174 The socket is nonblocking and a previous connection attempt has not yet
179 is not a valid open file descriptor.
182 No one listening on the remote address.
185 The socket structure address is outside the user's address space.
188 The socket is nonblocking and the connection cannot be completed
194 for completion by selecting the socket for writing.
197 indicates writability, use
205 completed successfully
207 is zero) or unsuccessfully
209 is one of the usual error codes listed here,
210 explaining the reason for the failure).
213 The system call was interrupted by a signal that was caught; see
215 .\" For TCP, the connection will complete asynchronously.
216 .\" See http://lkml.org/lkml/2005/7/12/254
219 The socket is already connected.
222 Network is unreachable.
227 does not refer to a socket.
230 The socket type does not support the requested communications protocol.
231 This error can occur, for example,
232 on an attempt to connect a UNIX domain datagram socket to a stream socket.
235 Timeout while attempting connection.
236 The server may be too
237 busy to accept new connections.
238 Note that for IP sockets the timeout may
239 be very long when syncookies are enabled on the server.
241 POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD, (the
243 function first appeared in 4.2BSD).
244 .\" SVr4 documents the additional
245 .\" general error codes
246 .\" .BR EADDRNOTAVAIL ,
248 .\" .BR EAFNOSUPPORT ,
255 .\" documents many additional error conditions not described here.
257 POSIX.1 does not require the inclusion of
259 and this header file is not required on Linux.
260 However, some historical (BSD) implementations required this header
261 file, and portable applications are probably wise to include it.
263 For background on the
270 fails, consider the state of the socket as unspecified.
271 Portable applications should close the socket and create a new one for
274 An example of the use of
284 .BR path_resolution (7)