1CONNECT(2) Linux Programmer's Manual CONNECT(2)
2
3
4
6 connect - initiate a connection on a socket
7
9 #include <sys/types.h> /* See NOTES */
10 #include <sys/socket.h>
11
12 int connect(int sockfd, const struct sockaddr *addr,
13 socklen_t addrlen);
14
16 The connect() system call connects the socket referred to by the file
17 descriptor sockfd to the address specified by addr. The addrlen argu‐
18 ment specifies the size of addr. The format of the address in addr is
19 determined by the address space of the socket sockfd; see socket(2) for
20 further details.
21
22 If the socket sockfd is of type SOCK_DGRAM, then addr is the address to
23 which datagrams are sent by default, and the only address from which
24 datagrams are received. If the socket is of type SOCK_STREAM or
25 SOCK_SEQPACKET, this call attempts to make a connection to the socket
26 that is bound to the address specified by addr.
27
28 Some protocol sockets (e.g., UNIX domain stream sockets) may success‐
29 fully connect() only once.
30
31 Some protocol sockets (e.g., datagram sockets in the UNIX and Internet
32 domains) may use connect() multiple times to change their association.
33
34 Some protocol sockets (e.g., TCP sockets as well as datagram sockets in
35 the UNIX and Internet domains) may dissolve the association by connect‐
36 ing to an address with the sa_family member of sockaddr set to
37 AF_UNSPEC; thereafter, the socket can be connected to another address.
38 (AF_UNSPEC is supported on Linux since kernel 2.2.)
39
41 If the connection or binding succeeds, zero is returned. On error, -1
42 is returned, and errno is set appropriately.
43
45 The following are general socket errors only. There may be other
46 domain-specific error codes.
47
48 EACCES For UNIX domain sockets, which are identified by pathname: Write
49 permission is denied on the socket file, or search permission is
50 denied for one of the directories in the path prefix. (See also
51 path_resolution(7).)
52
53 EACCES, EPERM
54 The user tried to connect to a broadcast address without having
55 the socket broadcast flag enabled or the connection request
56 failed because of a local firewall rule.
57
58 EACCES can also be returned if an SELinux policy denied a con‐
59 nection (for example, if there is a policy saying that an HTTP
60 proxy can only connect to ports associated with HTTP servers,
61 and the proxy tries to connect to a different port). dd
62
63 EADDRINUSE
64 Local address is already in use.
65
66 EADDRNOTAVAIL
67 (Internet domain sockets) The socket referred to by sockfd had
68 not previously been bound to an address and, upon attempting to
69 bind it to an ephemeral port, it was determined that all port
70 numbers in the ephemeral port range are currently in use. See
71 the discussion of /proc/sys/net/ipv4/ip_local_port_range in
72 ip(7).
73
74 EAFNOSUPPORT
75 The passed address didn't have the correct address family in its
76 sa_family field.
77
78 EAGAIN For nonblocking UNIX domain sockets, the socket is nonblocking,
79 and the connection cannot be completed immediately. For other
80 socket families, there are insufficient entries in the routing
81 cache.
82
83 EALREADY
84 The socket is nonblocking and a previous connection attempt has
85 not yet been completed.
86
87 EBADF sockfd is not a valid open file descriptor.
88
89 ECONNREFUSED
90 A connect() on a stream socket found no one listening on the
91 remote address.
92
93 EFAULT The socket structure address is outside the user's address
94 space.
95
96 EINPROGRESS
97 The socket is nonblocking and the connection cannot be completed
98 immediately. (UNIX domain sockets failed with EAGAIN instead.)
99 It is possible to select(2) or poll(2) for completion by select‐
100 ing the socket for writing. After select(2) indicates writabil‐
101 ity, use getsockopt(2) to read the SO_ERROR option at level
102 SOL_SOCKET to determine whether connect() completed successfully
103 (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the
104 usual error codes listed here, explaining the reason for the
105 failure).
106
107 EINTR The system call was interrupted by a signal that was caught; see
108 signal(7).
109
110 EISCONN
111 The socket is already connected.
112
113 ENETUNREACH
114 Network is unreachable.
115
116 ENOTSOCK
117 The file descriptor sockfd does not refer to a socket.
118
119 EPROTOTYPE
120 The socket type does not support the requested communications
121 protocol. This error can occur, for example, on an attempt to
122 connect a UNIX domain datagram socket to a stream socket.
123
124 ETIMEDOUT
125 Timeout while attempting connection. The server may be too busy
126 to accept new connections. Note that for IP sockets the timeout
127 may be very long when syncookies are enabled on the server.
128
130 POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD, (connect() first appeared in
131 4.2BSD).
132
134 POSIX.1 does not require the inclusion of <sys/types.h>, and this
135 header file is not required on Linux. However, some historical (BSD)
136 implementations required this header file, and portable applications
137 are probably wise to include it.
138
139 For background on the socklen_t type, see accept(2).
140
141 If connect() fails, consider the state of the socket as unspecified.
142 Portable applications should close the socket and create a new one for
143 reconnecting.
144
146 An example of the use of connect() is shown in getaddrinfo(3).
147
149 accept(2), bind(2), getsockname(2), listen(2), socket(2), path_resolu‐
150 tion(7), selinux(8)
151
153 This page is part of release 5.07 of the Linux man-pages project. A
154 description of the project, information about reporting bugs, and the
155 latest version of this page, can be found at
156 https://www.kernel.org/doc/man-pages/.
157
158
159
160Linux 2020-04-11 CONNECT(2)