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