1connect(2)                    System Calls Manual                   connect(2)
2
3
4

NAME

6       connect - initiate a connection on a socket
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

43       If  the connection or binding succeeds, zero is returned.  On error, -1
44       is returned, and errno is set to indicate the error.
45

ERRORS

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

STANDARDS

132       POSIX.1-2008.
133

HISTORY

135       POSIX.1-2001, SVr4, 4.4BSD, (connect() first appeared in 4.2BSD).
136

NOTES

138       If connect() fails, consider the state of the  socket  as  unspecified.
139       Portable  applications should close the socket and create a new one for
140       reconnecting.
141

EXAMPLES

143       An example of the use of connect() is shown in getaddrinfo(3).
144

SEE ALSO

146       accept(2), bind(2), getsockname(2), listen(2), socket(2),  path_resolu‐
147       tion(7), selinux(8)
148
149
150
151Linux man-pages 6.04              2023-03-30                        connect(2)
Impressum