1CONNECT(2)                 Linux Programmer's Manual                CONNECT(2)
2
3
4

NAME

6       connect - initiate a connection on a socket
7

SYNOPSIS

9       #include <sys/socket.h>
10
11       int connect(int sockfd, const struct sockaddr *addr,
12                   socklen_t addrlen);
13

DESCRIPTION

15       The  connect()  system call connects the socket referred to by the file
16       descriptor sockfd to the address specified by addr.  The addrlen  argu‐
17       ment  specifies the size of addr.  The format of the address in addr is
18       determined by the address space of the socket sockfd; see socket(2) for
19       further details.
20
21       If the socket sockfd is of type SOCK_DGRAM, then addr is the address to
22       which datagrams are sent by default, and the only  address  from  which
23       datagrams  are  received.   If  the  socket  is  of type SOCK_STREAM or
24       SOCK_SEQPACKET, this call attempts to make a connection to  the  socket
25       that is bound to the address specified by addr.
26
27       Some  protocol  sockets (e.g., UNIX domain stream sockets) may success‐
28       fully connect() only once.
29
30       Some protocol sockets (e.g., datagram sockets in the UNIX and  Internet
31       domains) may use connect() multiple times to change their association.
32
33       Some protocol sockets (e.g., TCP sockets as well as datagram sockets in
34       the UNIX and Internet domains) may dissolve the association by connect‐
35       ing  to  an address with the sa_family member of sockaddr set to AF_UN‐
36       SPEC; thereafter, the socket  can  be  connected  to  another  address.
37       (AF_UNSPEC is supported on Linux since kernel 2.2.)
38

RETURN VALUE

40       If  the connection or binding succeeds, zero is returned.  On error, -1
41       is returned, and errno is set to indicate the error.
42

ERRORS

44       The following are general socket errors only.  There may be  other  do‐
45       main-specific error codes.
46
47       EACCES For UNIX domain sockets, which are identified by pathname: Write
48              permission is denied on the socket file, or search permission is
49              denied for one of the directories in the path prefix.  (See also
50              path_resolution(7).)
51
52       EACCES, EPERM
53              The user tried to connect to a broadcast address without  having
54              the  socket  broadcast  flag  enabled  or the connection request
55              failed because of a local firewall rule.
56
57              EACCES can also be returned if an SELinux policy denied  a  con‐
58              nection  (for  example, if there is a policy saying that an HTTP
59              proxy can only connect to ports associated  with  HTTP  servers,
60              and the proxy tries to connect to a different port).  dd
61
62       EADDRINUSE
63              Local address is already in use.
64
65       EADDRNOTAVAIL
66              (Internet  domain  sockets) The socket referred to by sockfd had
67              not previously been bound to an address and, upon attempting  to
68              bind  it  to  an ephemeral port, it was determined that all port
69              numbers in the ephemeral port range are currently in  use.   See
70              the   discussion  of  /proc/sys/net/ipv4/ip_local_port_range  in
71              ip(7).
72
73       EAFNOSUPPORT
74              The passed address didn't have the correct address family in its
75              sa_family field.
76
77       EAGAIN For  nonblocking UNIX domain sockets, the socket is nonblocking,
78              and the connection cannot be completed immediately.   For  other
79              socket  families,  there are insufficient entries in the routing
80              cache.
81
82       EALREADY
83              The socket is nonblocking and a previous connection attempt  has
84              not yet been completed.
85
86       EBADF  sockfd is not a valid open file descriptor.
87
88       ECONNREFUSED
89              A connect() on a stream socket found no one listening on the re‐
90              mote address.
91
92       EFAULT The socket structure  address  is  outside  the  user's  address
93              space.
94
95       EINPROGRESS
96              The socket is nonblocking and the connection cannot be completed
97              immediately.  (UNIX domain sockets failed with EAGAIN  instead.)
98              It is possible to select(2) or poll(2) for completion by select‐
99              ing the socket for writing.  After select(2) indicates writabil‐
100              ity,  use  getsockopt(2)  to  read  the SO_ERROR option at level
101              SOL_SOCKET to determine whether connect() completed successfully
102              (SO_ERROR  is  zero)  or  unsuccessfully (SO_ERROR is one of the
103              usual error codes listed here, explaining  the  reason  for  the
104              failure).
105
106       EINTR  The system call was interrupted by a signal that was caught; see
107              signal(7).
108
109       EISCONN
110              The socket is already connected.
111
112       ENETUNREACH
113              Network is unreachable.
114
115       ENOTSOCK
116              The file descriptor sockfd does not refer to a socket.
117
118       EPROTOTYPE
119              The socket type does not support  the  requested  communications
120              protocol.   This  error can occur, for example, on an attempt to
121              connect a UNIX domain datagram socket to a stream socket.
122
123       ETIMEDOUT
124              Timeout while attempting connection.  The server may be too busy
125              to accept new connections.  Note that for IP sockets the timeout
126              may be very long when syncookies are enabled on the server.
127

CONFORMING TO

129       POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD, (connect() first appeared  in
130       4.2BSD).
131

NOTES

133       For background on the socklen_t type, see accept(2).
134
135       If  connect()  fails,  consider the state of the socket as unspecified.
136       Portable applications should close the socket and create a new one  for
137       reconnecting.
138

EXAMPLES

140       An example of the use of connect() is shown in getaddrinfo(3).
141

SEE ALSO

143       accept(2),  bind(2), getsockname(2), listen(2), socket(2), path_resolu‐
144       tion(7), selinux(8)
145

COLOPHON

147       This page is part of release 5.13 of the Linux  man-pages  project.   A
148       description  of  the project, information about reporting bugs, and the
149       latest    version    of    this    page,    can     be     found     at
150       https://www.kernel.org/doc/man-pages/.
151
152
153
154Linux                             2021-03-22                        CONNECT(2)
Impressum