1.\" $OpenBSD: connect.2,v 1.22 2009/12/29 10:25:43 sobrado Exp $ 2.\" $NetBSD: connect.2,v 1.8 1995/10/12 15:40:48 jtc Exp $ 3.\" 4.\" Copyright (c) 1983, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)connect.2 8.1 (Berkeley) 6/4/93 32.\" 33.Dd $Mdocdate: December 29 2009 $ 34.Dt CONNECT 2 35.Os 36.Sh NAME 37.Nm connect 38.Nd initiate a connection on a socket 39.Sh SYNOPSIS 40.Fd #include <sys/types.h> 41.Fd #include <sys/socket.h> 42.Ft int 43.Fn connect "int s" "const struct sockaddr *name" "socklen_t namelen" 44.Sh DESCRIPTION 45The parameter 46.Fa s 47is a socket. 48If it is of type 49.Dv SOCK_DGRAM , 50this call specifies the peer with which the socket is to be associated; 51this address is that to which datagrams are to be sent, 52and the only address from which datagrams are to be received. 53If the socket is of type 54.Dv SOCK_STREAM , 55this call attempts to make a connection to 56another socket. 57The other socket is specified by 58.Fa name , 59which is an address in the communications space of the socket. 60.Fa namelen 61indicates the amount of space pointed to by 62.Fa name , 63in bytes. 64Each communications space interprets the 65.Fa name 66parameter in its own way. 67Generally, stream sockets may use 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 EINVAL 99A TCP connection with a local broadcast, the all-ones or a 100multicast address as the peer was attempted. 101.It Bq Er ECONNREFUSED 102The attempt to connect was forcefully rejected. 103.It Bq Er EHOSTUNREACH 104The destination address specified an unreachable host. 105.It Bq Er EINTR 106A connect was interrupted before it succeeded 107by the delivery of a signal. 108.It Bq Er ENETUNREACH 109The network isn't reachable from this host. 110.It Bq Er EADDRINUSE 111The address is already in use. 112.It Bq Er EFAULT 113The 114.Fa name 115parameter specifies an area outside 116the process address space. 117.It Bq Er EINPROGRESS 118The socket is non-blocking 119and the connection cannot 120be completed immediately. 121It is possible to 122.Xr select 2 123or 124.Xr poll 2 125for completion by selecting the socket for writing, and also use 126.Xr getsockopt 2 127with 128.Dv SO_ERROR 129to check for error conditions. 130.It Bq Er EALREADY 131The socket is non-blocking 132and a previous connection attempt 133has not yet been completed. 134.El 135.Pp 136The following errors are specific to connecting names in the 137.Ux Ns -domain . 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. 154.It Bq Er EACCES 155Write access to the named socket is denied. 156.It Bq Er ELOOP 157Too many symbolic links were encountered in translating the pathname. 158.It Bq Er EPROTOTYPE 159The file described by 160.Fa name 161is of a different type than 162.Fa s . 163E.g., 164.Fa s 165may be of type 166.Dv SOCK_STREAM 167whereas 168.Fa name 169may refer to a socket of type 170.Dv SOCK_DGRAM . 171.El 172.Sh SEE ALSO 173.Xr accept 2 , 174.Xr getsockname 2 , 175.Xr getsockopt 2 , 176.Xr poll 2 , 177.Xr select 2 , 178.Xr socket 2 179.Sh HISTORY 180The 181.Fn connect 182function call appeared in 183.Bx 4.2 . 184