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 Generally, connection-based protocol sockets may successfully connect()
29 only once; connectionless protocol sockets may use connect() multiple
30 times to change their association. Connectionless sockets may dissolve
31 the association by connecting to an address with the sa_family member
32 of sockaddr set to AF_UNSPEC (supported on Linux since kernel 2.2).
33
35 If the connection or binding succeeds, zero is returned. On error, -1
36 is returned, and errno is set appropriately.
37
39 The following are general socket errors only. There may be other
40 domain-specific error codes.
41
42 EACCES For Unix domain sockets, which are identified by pathname: Write
43 permission is denied on the socket file, or search permission is
44 denied for one of the directories in the path prefix. (See also
45 path_resolution(7).)
46
47 EACCES, EPERM
48 The user tried to connect to a broadcast address without having
49 the socket broadcast flag enabled or the connection request
50 failed because of a local firewall rule.
51
52 EADDRINUSE
53 Local address is already in use.
54
55 EAFNOSUPPORT
56 The passed address didn't have the correct address family in its
57 sa_family field.
58
59 EAGAIN No more free local ports or insufficient entries in the routing
60 cache. For AF_INET see the description of
61 /proc/sys/net/ipv4/ip_local_port_range ip(7) for information on
62 how to increase the number of local ports.
63
64 EALREADY
65 The socket is nonblocking and a previous connection attempt has
66 not yet been completed.
67
68 EBADF The file descriptor is not a valid index in the descriptor ta‐
69 ble.
70
71 ECONNREFUSED
72 No-one listening on the remote address.
73
74 EFAULT The socket structure address is outside the user's address
75 space.
76
77 EINPROGRESS
78 The socket is nonblocking and the connection cannot be completed
79 immediately. It is possible to select(2) or poll(2) for comple‐
80 tion by selecting the socket for writing. After select(2) indi‐
81 cates writability, use getsockopt(2) to read the SO_ERROR option
82 at level SOL_SOCKET to determine whether connect() completed
83 successfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is
84 one of the usual error codes listed here, explaining the reason
85 for the failure).
86
87 EINTR The system call was interrupted by a signal that was caught; see
88 signal(7).
89
90 EISCONN
91 The socket is already connected.
92
93 ENETUNREACH
94 Network is unreachable.
95
96 ENOTSOCK
97 The file descriptor is not associated with a socket.
98
99 ETIMEDOUT
100 Timeout while attempting connection. The server may be too busy
101 to accept new connections. Note that for IP sockets the timeout
102 may be very long when syncookies are enabled on the server.
103
105 SVr4, 4.4BSD, (the connect() function first appeared in 4.2BSD),
106 POSIX.1-2001.
107
109 POSIX.1-2001 does not require the inclusion of <sys/types.h>, and this
110 header file is not required on Linux. However, some historical (BSD)
111 implementations required this header file, and portable applications
112 are probably wise to include it.
113
114 The third argument of connect() is in reality an int (and this is what
115 4.x BSD and libc4 and libc5 have). Some POSIX confusion resulted in
116 the present socklen_t, also used by glibc. See also accept(2).
117
119 An example of the use of connect() is shown in getaddrinfo(3).
120
122 accept(2), bind(2), getsockname(2), listen(2), socket(2), path_resolu‐
123 tion(7)
124
126 This page is part of release 3.25 of the Linux man-pages project. A
127 description of the project, and information about reporting bugs, can
128 be found at http://www.kernel.org/doc/man-pages/.
129
130
131
132Linux 2008-12-03 CONNECT(2)