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 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
8 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
10 .\" Copyright (c) 1983 The Regents of the University of California.
11 .\" All rights reserved.
13 .\" SPDX-License-Identifier: BSD-4-Clause-UC
15 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
16 .\" Modified 1998, 1999 by Andi Kleen
17 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
19 .TH CONNECT 2 2022-09-09 "Linux man-pages (unreleased)"
21 connect \- initiate a connection on a socket
24 .RI ( libc ", " \-lc )
27 .B #include <sys/socket.h>
29 .BI "int connect(int " sockfd ", const struct sockaddr *" addr ,
30 .BI " socklen_t " addrlen );
35 system call connects the socket referred to by the file descriptor
37 to the address specified by
41 argument specifies the size of
43 The format of the address in
45 is determined by the address space of the socket
57 is the address to which datagrams are sent by default, and the only
58 address from which datagrams are received.
59 If the socket is of type
63 this call attempts to make a connection to the socket that is bound
64 to the address specified by
67 Some protocol sockets (e.g., UNIX domain stream sockets)
73 (e.g., datagram sockets in the UNIX and Internet domains)
76 multiple times to change their association.
79 (e.g., TCP sockets as well as datagram sockets in the UNIX and
81 may dissolve the association by connecting to an address with the
87 thereafter, the socket can be connected to another address.
89 is supported on Linux since kernel 2.2.)
91 If the connection or binding succeeds, zero is returned.
92 On error, \-1 is returned, and
94 is set to indicate the error.
96 The following are general socket errors only.
97 There may be other domain-specific error codes.
100 For UNIX domain sockets, which are identified by pathname:
101 Write permission is denied on the socket file,
102 or search permission is denied for one of the directories
105 .BR path_resolution (7).)
107 .BR EACCES ", " EPERM
108 The user tried to connect to a broadcast address without having the socket
109 broadcast flag enabled or the connection request failed because of a local
113 can also be returned if an SELinux policy denied a connection (for
114 example, if there is a policy saying that an HTTP proxy can only
115 connect to ports associated with HTTP servers, and the proxy tries to
116 connect to a different port).
120 Local address is already in use.
123 (Internet domain sockets)
124 The socket referred to by
126 had not previously been bound to an address and,
127 upon attempting to bind it to an ephemeral port,
128 it was determined that all port numbers in the ephemeral port range
129 are currently in use.
130 See the discussion of
131 .I /proc/sys/net/ipv4/ip_local_port_range
136 The passed address didn't have the correct address family in its
141 For nonblocking UNIX domain sockets, the socket is nonblocking, and the
142 connection cannot be completed immediately.
143 For other socket families, there are insufficient entries in the routing cache.
146 The socket is nonblocking and a previous connection attempt has not yet
151 is not a valid open file descriptor.
156 on a stream socket found no one listening on the remote address.
159 The socket structure address is outside the user's address space.
162 The socket is nonblocking and the connection cannot be completed immediately.
163 (UNIX domain sockets failed with
170 for completion by selecting the socket for writing.
173 indicates writability, use
181 completed successfully
183 is zero) or unsuccessfully
185 is one of the usual error codes listed here,
186 explaining the reason for the failure).
189 The system call was interrupted by a signal that was caught; see
191 .\" For TCP, the connection will complete asynchronously.
192 .\" See http://lkml.org/lkml/2005/7/12/254
195 The socket is already connected.
198 Network is unreachable.
203 does not refer to a socket.
206 The socket type does not support the requested communications protocol.
207 This error can occur, for example,
208 on an attempt to connect a UNIX domain datagram socket to a stream socket.
211 Timeout while attempting connection.
212 The server may be too
213 busy to accept new connections.
214 Note that for IP sockets the timeout may
215 be very long when syncookies are enabled on the server.
217 POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD,
219 first appeared in 4.2BSD).
220 .\" SVr4 documents the additional
221 .\" general error codes
222 .\" .BR EADDRNOTAVAIL ,
224 .\" .BR EAFNOSUPPORT ,
231 .\" documents many additional error conditions not described here.
233 For background on the
240 fails, consider the state of the socket as unspecified.
241 Portable applications should close the socket and create a new one for
244 An example of the use of
254 .BR path_resolution (7),