1CONNECT(P)                 POSIX Programmer's Manual                CONNECT(P)
2
3
4

NAME

6       connect - connect a socket
7

SYNOPSIS

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

DESCRIPTION

16       The  connect() function shall attempt to make a connection on a socket.
17       The function takes the following arguments:
18
19       socket Specifies the file descriptor associated with the socket.
20
21       address
22              Points to a sockaddr structure containing the peer address.  The
23              length and format of the address depend on the address family of
24              the socket.
25
26       address_len
27              Specifies the length of the sockaddr structure pointed to by the
28              address argument.
29
30
31       If  the socket has not already been bound to a local address, connect()
32       shall bind it to an address which, unless the socket's  address  family
33       is AF_UNIX, is an unused local address.
34
35       If  the  initiating socket is not connection-mode, then connect() shall
36       set  the  socket's  peer  address,  and  no  connection  is  made.  For
37       SOCK_DGRAM sockets, the peer address identifies where all datagrams are
38       sent on subsequent send() functions, and limits the remote  sender  for
39       subsequent  recv() functions. If address is a null address for the pro‐
40       tocol, the socket's peer address shall be reset.
41
42       If the initiating  socket  is  connection-mode,  then  connect()  shall
43       attempt  to  establish  a  connection  to  the address specified by the
44       address argument. If the connection cannot be  established  immediately
45       and  O_NONBLOCK is not set for the file descriptor for the socket, con‐
46       nect() shall block for up to an unspecified timeout interval until  the
47       connection  is  established. If the timeout interval expires before the
48       connection is established, connect()  shall  fail  and  the  connection
49       attempt  shall be aborted. If connect() is interrupted by a signal that
50       is caught while blocked waiting to establish  a  connection,  connect()
51       shall  fail  and set errno to [EINTR], but the connection request shall
52       not be aborted, and the connection shall be established asynchronously.
53
54       If the connection cannot be established immediately and  O_NONBLOCK  is
55       set  for  the  file descriptor for the socket, connect() shall fail and
56       set errno to [EINPROGRESS], but the connection  request  shall  not  be
57       aborted, and the connection shall be established asynchronously. Subse‐
58       quent calls to connect() for the same socket, before the connection  is
59       established, shall fail and set errno to [EALREADY].
60
61       When  the  connection has been established asynchronously, select() and
62       poll() shall indicate that the file descriptor for the socket is  ready
63       for writing.
64
65       The  socket  in  use may require the process to have appropriate privi‐
66       leges to use the connect() function.
67

RETURN VALUE

69       Upon successful completion, connect() shall  return  0;  otherwise,  -1
70       shall be returned and errno set to indicate the error.
71

ERRORS

73       The connect() function shall fail if:
74
75       EADDRNOTAVAIL
76              The specified address is not available from the local machine.
77
78       EAFNOSUPPORT
79              The  specified  address  is  not a valid address for the address
80              family of the specified socket.
81
82       EALREADY
83              A connection request is already in progress  for  the  specified
84              socket.
85
86       EBADF  The socket argument is not a valid file descriptor.
87
88       ECONNREFUSED
89              The  target address was not listening for connections or refused
90              the connection request.
91
92       EINPROGRESS
93              O_NONBLOCK is set for the file descriptor for the socket and the
94              connection  cannot  be  immediately  established; the connection
95              shall be established asynchronously.
96
97       EINTR  The attempt to establish a connection was interrupted by  deliv‐
98              ery  of a signal that was caught; the connection shall be estab‐
99              lished asynchronously.
100
101       EISCONN
102              The specified socket is  connection-mode  and  is  already  con‐
103              nected.
104
105       ENETUNREACH
106              No route to the network is present.
107
108       ENOTSOCK
109              The socket argument does not refer to a socket.
110
111       EPROTOTYPE
112              The specified address has a different type than the socket bound
113              to the specified peer address.
114
115       ETIMEDOUT
116              The attempt to connect timed out before a connection was made.
117
118
119       If the address family of the socket is AF_UNIX,  then  connect()  shall
120       fail if:
121
122       EIO    An  I/O error occurred while reading from or writing to the file
123              system.
124
125       ELOOP  A loop exists in symbolic links encountered during resolution of
126              the pathname in address.
127
128       ENAMETOOLONG
129              A  component of a pathname exceeded {NAME_MAX} characters, or an
130              entire pathname exceeded {PATH_MAX} characters.
131
132       ENOENT A component of the pathname does not name an  existing  file  or
133              the pathname is an empty string.
134
135       ENOTDIR
136              A component of the path prefix of the pathname in address is not
137              a directory.
138
139
140       The connect() function may fail if:
141
142       EACCES Search permission is denied for a component of the path  prefix;
143              or write access to the named socket is denied.
144
145       EADDRINUSE
146              Attempt  to  establish a connection that uses addresses that are
147              already in use.
148
149       ECONNRESET
150              Remote host reset the connection request.
151
152       EHOSTUNREACH
153              The destination host cannot be  reached  (probably  because  the
154              host is down or a remote router cannot reach it).
155
156       EINVAL The  address_len  argument is not a valid length for the address
157              family; or invalid address family in the sockaddr structure.
158
159       ELOOP  More than {SYMLOOP_MAX} symbolic links were  encountered  during
160              resolution of the pathname in address.
161
162       ENAMETOOLONG
163              Pathname  resolution of a symbolic link produced an intermediate
164              result whose length exceeds {PATH_MAX}.
165
166       ENETDOWN
167              The local network interface used to  reach  the  destination  is
168              down.
169
170       ENOBUFS
171              No buffer space is available.
172
173       EOPNOTSUPP
174              The socket is listening and cannot be connected.
175
176
177       The following sections are informative.
178

EXAMPLES

180       None.
181

APPLICATION USAGE

183       If connect() fails, the state of the socket is unspecified.  Conforming
184       applications should close the file descriptor and create a  new  socket
185       before attempting to reconnect.
186

RATIONALE

188       None.
189

FUTURE DIRECTIONS

191       None.
192

SEE ALSO

194       accept()  ,  bind()  ,  close()  ,  getsockname() , poll() , select() ,
195       send() , shutdown()  ,  socket()  ,  the  Base  Definitions  volume  of
196       IEEE Std 1003.1-2001, <sys/socket.h>
197
199       Portions  of  this text are reprinted and reproduced in electronic form
200       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
201       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
202       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
203       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
204       event of any discrepancy between this version and the original IEEE and
205       The  Open Group Standard, the original IEEE and The Open Group Standard
206       is the referee document. The original Standard can be  obtained  online
207       at http://www.opengroup.org/unix/online.html .
208
209
210
211IEEE/The Open Group                  2003                           CONNECT(P)
Impressum