1.\" $NetBSD: connect.2,v 1.20 2002/02/08 01:28:17 ross 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 October 16, 2001 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 \*[Lt]sys/socket.h\*[Gt] 46.Ft int 47.Fn connect "int s" "const struct sockaddr *name" "socklen_t namelen" 48.Sh DESCRIPTION 49The parameter 50.Fa s 51is a socket. 52If it is of type 53.Dv SOCK_DGRAM , 54this call specifies the peer with which the socket is to be associated; 55this address is that to which datagrams are to be sent, 56and the only address from which datagrams are to be received. 57If the socket is of type 58.Dv SOCK_STREAM , 59this call attempts to make a connection to 60another socket. 61The other socket is specified by 62.Fa name , 63which is an address in the communications space of the socket. 64Each communications space interprets the 65.Fa name 66parameter in its own way. 67Generally, stream sockets may successfully 68.Fn connect 69only once; datagram sockets may use 70.Fn connect 71multiple times to change their association. 72Datagram sockets may dissolve the association 73by connecting to an invalid address, such as a null address. 74.Sh RETURN VALUES 75If the connection or binding succeeds, 0 is returned. 76Otherwise a -1 is returned, and a more specific error 77code is stored in 78.Va errno . 79.Sh ERRORS 80The 81.Fn connect 82call fails if: 83.Bl -tag -width Er 84.It Bq Er EBADF 85.Fa s 86is not a valid descriptor. 87.It Bq Er ENOTSOCK 88.Fa s 89is a descriptor for a file, not a socket. 90.It Bq Er EADDRNOTAVAIL 91The specified address is not available on this machine. 92.It Bq Er EAFNOSUPPORT 93Addresses in the specified address family cannot be used with this socket. 94.It Bq Er EISCONN 95The socket is already connected. 96.It Bq Er ETIMEDOUT 97Connection establishment timed out without establishing a connection. 98.It Bq Er ECONNREFUSED 99The attempt to connect was forcefully rejected. 100.It Bq Er ENETUNREACH 101The network isn't reachable from this host. 102.It Bq Er EADDRINUSE 103The address is already in use. 104.It Bq Er EFAULT 105The 106.Fa name 107parameter specifies an area outside 108the process address space. 109.It Bq Er EINPROGRESS 110The socket is non-blocking and the connection cannot be completed immediately. 111It is possible to 112.Xr select 2 113or 114.Xr poll 2 115for completion by selecting or polling the socket for writing. 116The success or failure of the connect operation may be determined by using 117.Xr getsockopt 2 118to read the socket error status with the 119.Dv SO_ERROR 120option at the 121.Dv SOL_SOCKET 122level. 123The returned socket error status is zero on success, or one of the 124error codes listed here on failure. 125.It Bq Er EALREADY 126The socket is non-blocking 127and a previous connection attempt 128has not yet been completed. 129.El 130.Pp 131The following errors are specific to connecting names in the 132.Ux 133domain. 134These errors may not apply in future versions of the 135.Ux 136IPC domain. 137.Bl -tag -width Er 138.It Bq Er ENOTDIR 139A component of the path prefix is not a directory. 140.It Bq Er ENAMETOOLONG 141A component of a pathname exceeded 142.Dv {NAME_MAX} 143characters, or an entire path name exceeded 144.Dv {PATH_MAX} 145characters. 146.It Bq Er ENOENT 147The named socket does not exist. 148.It Bq Er EACCES 149Search permission is denied for a component of the path prefix, or 150write access to the named socket is denied. 151.It Bq Er ELOOP 152Too many symbolic links were encountered in translating the pathname. 153.El 154.Sh SEE ALSO 155.Xr accept 2 , 156.Xr getsockname 2 , 157.Xr getsockopt 2 , 158.Xr poll 2 , 159.Xr select 2 , 160.Xr socket 2 161.Sh HISTORY 162The 163.Fn connect 164function call appeared in 165.Bx 4.2 . 166