xref: /original-bsd/lib/libc/sys/connect.2 (revision c3e32dec)
1.\" Copyright (c) 1983, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)connect.2	8.1 (Berkeley) 06/04/93
7.\"
8.Dd
9.Dt CONNECT 2
10.Os BSD 4.2
11.Sh NAME
12.Nm connect
13.Nd initiate a connection on a socket
14.Sh SYNOPSIS
15.Fd #include <sys/types.h>
16.Fd #include <sys/socket.h>
17.Ft int
18.Fn connect "int s" "struct sockaddr *name" "int namelen"
19.Sh DESCRIPTION
20The parameter
21.Fa s
22is a socket.
23If it is of type
24.Dv SOCK_DGRAM ,
25this call specifies the peer with which the socket is to be associated;
26this address is that to which datagrams are to be sent,
27and the only address from which datagrams are to be received.
28If the socket is of type
29.Dv SOCK_STREAM ,
30this call attempts to make a connection to
31another socket.
32The other socket is specified by
33.Fa name ,
34which is an address in the communications space of the socket.
35Each communications space interprets the
36.Fa name
37parameter in its own way.
38Generally, stream sockets may successfully
39.Fn connect
40only once; datagram sockets may use
41.Fn connect
42multiple times to change their association.
43Datagram sockets may dissolve the association
44by connecting to an invalid address, such as a null address.
45.Sh RETURN VALUES
46If the connection or binding succeeds, 0 is returned.
47Otherwise a -1 is returned, and a more specific error
48code is stored in
49.Va errno .
50.Sh ERRORS
51The
52.Fn connect
53call fails if:
54.Bl -tag -width EADDRNOTAVAILABB
55.It Bq Er EBADF
56.Fa S
57is not a valid descriptor.
58.It Bq Er ENOTSOCK
59.Fa S
60is a descriptor for a file, not a socket.
61.It Bq Er EADDRNOTAVAIL
62The specified address is not available on this machine.
63.It Bq Er EAFNOSUPPORT
64Addresses in the specified address family cannot be used with this socket.
65.It Bq Er EISCONN
66The socket is already connected.
67.It Bq Er ETIMEDOUT
68Connection establishment timed out without establishing a connection.
69.It Bq Er ECONNREFUSED
70The attempt to connect was forcefully rejected.
71.It Bq Er ENETUNREACH
72The network isn't reachable from this host.
73.It Bq Er EADDRINUSE
74The address is already in use.
75.It Bq Er EFAULT
76The
77.Fa name
78parameter specifies an area outside
79the process address space.
80.It Bq Er EINPROGRESS
81The socket is non-blocking
82and the connection cannot
83be completed immediately.
84It is possible to
85.Xr select 2
86for completion by selecting the socket for writing.
87.It Bq Er EALREADY
88The socket is non-blocking
89and a previous connection attempt
90has not yet been completed.
91.El
92.Pp
93The following errors are specific to connecting names in the UNIX domain.
94These errors may not apply in future versions of the UNIX IPC domain.
95.Bl -tag -width EADDRNOTAVAILABB
96.It Bq Er ENOTDIR
97A component of the path prefix is not a directory.
98.It Bq Er EINVAL
99The pathname contains a character with the high-order bit set.
100.It Bq Er ENAMETOOLONG
101A component of a pathname exceeded 255 characters,
102or an entire path name exceeded 1023 characters.
103.It Bq Er ENOENT
104The named socket does not exist.
105.It Bq Er EACCES
106Search permission is denied for a component of the path prefix.
107.It Bq Er EACCES
108Write access to the named socket is denied.
109.It Bq Er ELOOP
110Too many symbolic links were encountered in translating the pathname.
111.El
112.Sh SEE ALSO
113.Xr accept 2 ,
114.Xr select 2 ,
115.Xr socket 2 ,
116.Xr getsockname 2
117.Sh HISTORY
118The
119.Nm
120function call appeared in
121.Bx 4.2 .
122