1connect(3XNET)   X/Open Networking Services Library Functions   connect(3XNET)
2
3
4

NAME

6       connect - connect a socket
7

SYNOPSIS

9       cc [ flag ... ] file ... -lxnet [ library ... ]
10       #include <sys/socket.h>
11
12       int connect(int socket, const struct sockaddr *address,
13           socklen_t address_len);
14
15

DESCRIPTION

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

USAGE

80       If  connect()  fails, the state of the socket is unspecified.  Portable
81       applications should close the file descriptor and create a  new  socket
82       before attempting to reconnect.
83

RETURN VALUES

85       Upon  successful  completion,  connect()  returns  0.  Otherwise, −1 is
86       returned and errno is set to indicate the error.
87

ERRORS

89       The connect() function will fail if:
90
91       EADDRNOTAVAIL    The specified address is not available from the  local
92                        machine.
93
94
95       EAFNOSUPPORT     The  specified  address is not a valid address for the
96                        address family of the specified socket.
97
98
99       EALREADY         A connection request is already in  progress  for  the
100                        specified socket.
101
102
103       EBADF            The socket argument is not a valid file descriptor.
104
105
106       ECONNREFUSED     The  target  address was not listening for connections
107                        or refused the connection request.
108
109
110       EFAULT           The address parameter can not be accessed.
111
112
113       EINPROGRESS      O_NONBLOCK is set for  the  file  descriptor  for  the
114                        socket and the connection cannot be immediately estab‐
115                        lished;  the  connection  will  be  established  asyn‐
116                        chronously.
117
118
119       EINTR            The  attempt to establish a connection was interrupted
120                        by delivery of a signal that was caught;  the  connec‐
121                        tion will be established asynchronously.
122
123
124       EISCONN          The specified socket is connection-mode and is already
125                        connected.
126
127
128       ENETUNREACH      No route to the network is present.
129
130
131       ENOTSOCK         The socket argument does not refer to a socket.
132
133
134       EPROTOTYPE       The specified address has a different  type  than  the
135                        socket bound to the specified peer address.
136
137
138       ETIMEDOUT        The  attempt  to connect timed out before a connection
139                        was made.
140
141
142
143       If the address family of the socket is  AF_UNIX,  then  connect()  will
144       fail if:
145
146       EIO             An  I/O error occurred while reading from or writing to
147                       the file system.
148
149
150       ELOOP           Too many symbolic links were encountered in translating
151                       the pathname in address.
152
153
154       ENAMETOOLONG    A component of a pathname exceeded NAME_MAX characters,
155                       or an entire pathname exceeded PATH_MAX characters.
156
157
158       ENOENT          A component of the pathname does not name  an  existing
159                       file or the pathname is an empty string.
160
161
162       ENOTDIR         A  component  of  the  path  prefix  of the pathname in
163                       address is not a directory.
164
165
166
167       The connect() function may fail if:
168
169       EACCES          Search permission is denied for a component of the path
170                       prefix; or write access to the named socket is denied.
171
172
173       EADDRINUSE      Attempt  to  establish a connection that uses addresses
174                       that are already in use.
175
176
177       ECONNRESET      Remote host reset the connection request.
178
179
180       EHOSTUNREACH    The  destination  host  cannot  be  reached   (probably
181                       because  the  host  is  down  or a remote router cannot
182                       reach it).
183
184
185       EINVAL          The address_len argument is not a valid length for  the
186                       address  family;  or invalid address family in sockaddr
187                       structure.
188
189
190       ENAMETOOLONG    Pathname resolution of  a  symbolic  link  produced  an
191                       intermediate result whose length exceeds PATH_MAX.
192
193
194       ENETDOWN        The  local  interface  used to reach the destination is
195                       down.
196
197
198       ENOBUFS         No buffer space is available.
199
200
201       ENOSR           There were insufficient STREAMS resources available  to
202                       complete the operation.
203
204
205       EOPNOTSUPP      The socket is listening and can not be connected.
206
207

ATTRIBUTES

209       See attributes(5) for descriptions of the following attributes:
210
211
212
213
214       ┌─────────────────────────────┬─────────────────────────────┐
215       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
216       ├─────────────────────────────┼─────────────────────────────┤
217       │Interface Stability          │Standard                     │
218       ├─────────────────────────────┼─────────────────────────────┤
219       │MT-Level                     │MT-Safe                      │
220       └─────────────────────────────┴─────────────────────────────┘
221

SEE ALSO

223       close(2),   poll(2),  accept(3XNET),  bind(3XNET),  getsockname(3XNET),
224       select(3C), send(3XNET), shutdown(3XNET), socket(3XNET), attributes(5),
225       standards(5)
226
227
228
229SunOS 5.11                        10 Jun 2002                   connect(3XNET)
Impressum