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/types.h>          /* See NOTES */
10       #include <sys/socket.h>
11
12       int connect(int sockfd, const struct sockaddr *addr,
13                   socklen_t addrlen);
14

DESCRIPTION

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

RETURN VALUE

35       If  the connection or binding succeeds, zero is returned.  On error, -1
36       is returned, and errno is set appropriately.
37

ERRORS

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       EADDRNOTAVAIL
56              (Internet domain sockets) The socket referred to by  sockfd  had
57              not  previously been bound to an address and, upon attempting to
58              bind it to an ephemeral port, it was determined  that  all  port
59              numbers  in  the ephemeral port range are currently in use.  See
60              the  discussion  of  /proc/sys/net/ipv4/ip_local_port_range   in
61              ip(7).
62
63       EAFNOSUPPORT
64              The passed address didn't have the correct address family in its
65              sa_family field.
66
67       EAGAIN For nonblocking UNIX domain sockets, the socket is  nonblocking,
68              and  the  connection cannot be completed immediately.  For other
69              socket families, there are insufficient entries in  the  routing
70              cache.
71
72       EALREADY
73              The  socket is nonblocking and a previous connection attempt has
74              not yet been completed.
75
76       EBADF  sockfd is not a valid open file descriptor.
77
78       ECONNREFUSED
79              A connect() on a stream socket found no  one  listening  on  the
80              remote address.
81
82       EFAULT The  socket  structure  address  is  outside  the user's address
83              space.
84
85       EINPROGRESS
86              The socket is nonblocking and the connection cannot be completed
87              immediately.   (UNIX domain sockets failed with EAGAIN instead.)
88              It is possible to select(2) or poll(2) for completion by select‐
89              ing the socket for writing.  After select(2) indicates writabil‐
90              ity, use getsockopt(2) to read  the  SO_ERROR  option  at  level
91              SOL_SOCKET to determine whether connect() completed successfully
92              (SO_ERROR is zero) or unsuccessfully (SO_ERROR  is  one  of  the
93              usual  error  codes  listed  here, explaining the reason for the
94              failure).
95
96       EINTR  The system call was interrupted by a signal that was caught; see
97              signal(7).
98
99       EISCONN
100              The socket is already connected.
101
102       ENETUNREACH
103              Network is unreachable.
104
105       ENOTSOCK
106              The file descriptor sockfd does not refer to a socket.
107
108       EPROTOTYPE
109              The  socket  type  does not support the requested communications
110              protocol.  This error can occur, for example, on an  attempt  to
111              connect a UNIX domain datagram socket to a stream socket.
112
113       ETIMEDOUT
114              Timeout while attempting connection.  The server may be too busy
115              to accept new connections.  Note that for IP sockets the timeout
116              may be very long when syncookies are enabled on the server.
117

CONFORMING TO

119       POSIX.1-2001,  POSIX.1-2008, SVr4, 4.4BSD, (connect() first appeared in
120       4.2BSD).
121

NOTES

123       POSIX.1 does not require  the  inclusion  of  <sys/types.h>,  and  this
124       header  file  is not required on Linux.  However, some historical (BSD)
125       implementations required this header file,  and  portable  applications
126       are probably wise to include it.
127
128       For background on the socklen_t type, see accept(2).
129
130       If  connect()  fails,  consider the state of the socket as unspecified.
131       Portable applications should close the socket and create a new one  for
132       reconnecting.
133

EXAMPLE

135       An example of the use of connect() is shown in getaddrinfo(3).
136

SEE ALSO

138       accept(2),  bind(2), getsockname(2), listen(2), socket(2), path_resolu‐
139       tion(7)
140

COLOPHON

142       This page is part of release 5.02 of the Linux  man-pages  project.   A
143       description  of  the project, information about reporting bugs, and the
144       latest    version    of    this    page,    can     be     found     at
145       https://www.kernel.org/doc/man-pages/.
146
147
148
149Linux                             2019-03-06                        CONNECT(2)
Impressum