xref: /netbsd-src/lib/libc/sys/connect.2 (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1.\"	$NetBSD: connect.2,v 1.17 2000/06/17 19:33:34 hubertf Exp $
2.\"
3.\" Copyright (c) 1983, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)connect.2	8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993
37.Dt CONNECT 2
38.Os
39.Sh NAME
40.Nm connect
41.Nd initiate a connection on a socket
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.Fd #include <sys/types.h>
46.Fd #include <sys/socket.h>
47.Ft int
48.Fn connect "int s" "const struct sockaddr *name" "socklen_t namelen"
49.Sh DESCRIPTION
50The parameter
51.Fa s
52is a socket.
53If it is of type
54.Dv SOCK_DGRAM ,
55this call specifies the peer with which the socket is to be associated;
56this address is that to which datagrams are to be sent,
57and the only address from which datagrams are to be received.
58If the socket is of type
59.Dv SOCK_STREAM ,
60this call attempts to make a connection to
61another socket.
62The other socket is specified by
63.Fa name ,
64which is an address in the communications space of the socket.
65Each communications space interprets the
66.Fa name
67parameter in its own way.
68Generally, stream sockets may successfully
69.Fn connect
70only once; datagram sockets may use
71.Fn connect
72multiple times to change their association.
73Datagram sockets may dissolve the association
74by connecting to an invalid address, such as a null address.
75.Sh RETURN VALUES
76If the connection or binding succeeds, 0 is returned.
77Otherwise a -1 is returned, and a more specific error
78code is stored in
79.Va errno .
80.Sh ERRORS
81The
82.Fn connect
83call fails if:
84.Bl -tag -width Er
85.It Bq Er EBADF
86.Fa s
87is not a valid descriptor.
88.It Bq Er ENOTSOCK
89.Fa s
90is a descriptor for a file, not a socket.
91.It Bq Er EADDRNOTAVAIL
92The specified address is not available on this machine.
93.It Bq Er EAFNOSUPPORT
94Addresses in the specified address family cannot be used with this socket.
95.It Bq Er EISCONN
96The socket is already connected.
97.It Bq Er ETIMEDOUT
98Connection establishment timed out without establishing a connection.
99.It Bq Er ECONNREFUSED
100The attempt to connect was forcefully rejected.
101.It Bq Er ENETUNREACH
102The network isn't reachable from this host.
103.It Bq Er EADDRINUSE
104The address is already in use.
105.It Bq Er EFAULT
106The
107.Fa name
108parameter specifies an area outside
109the process address space.
110.It Bq Er EINPROGRESS
111The socket is non-blocking and the connection cannot be completed immediately.
112It is possible to
113.Xr select 2
114or
115.Xr poll 2
116for completion by selecting or polling the socket for writing.
117The success or failure of the connect operation may be determined by using
118.Xr getsockopt 2
119to read the socket error status with the
120.Dv SO_ERROR
121option at the
122.Dv SOL_SOCKET
123level.
124The returned socket error status is zero on success, or one of the
125error codes listed here on failure.
126.It Bq Er EALREADY
127The socket is non-blocking
128and a previous connection attempt
129has not yet been completed.
130.El
131.Pp
132The following errors are specific to connecting names in the
133.Ux
134domain.
135These errors may not apply in future versions of the
136.Ux
137IPC domain.
138.Bl -tag -width Er
139.It Bq Er ENOTDIR
140A component of the path prefix is not a directory.
141.It Bq Er ENAMETOOLONG
142A component of a pathname exceeded
143.Dv {NAME_MAX}
144characters, or an entire path name exceeded
145.Dv {PATH_MAX}
146characters.
147.It Bq Er ENOENT
148The named socket does not exist.
149.It Bq Er EACCES
150Search permission is denied for a component of the path prefix, or
151write access to the named socket is denied.
152.It Bq Er ELOOP
153Too many symbolic links were encountered in translating the pathname.
154.El
155.Sh SEE ALSO
156.Xr accept 2 ,
157.Xr poll 2 ,
158.Xr select 2 ,
159.Xr socket 2 ,
160.Xr getsockname 2 ,
161.Xr getsockopt 2
162.Sh HISTORY
163The
164.Fn connect
165function call appeared in
166.Bx 4.2 .
167