1.\" $OpenBSD: connect.2,v 1.25 2015/01/19 15:54:11 millert 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: January 19 2015 $ 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/socket.h> 41.Ft int 42.Fn connect "int s" "const struct sockaddr *name" "socklen_t namelen" 43.Sh DESCRIPTION 44The parameter 45.Fa s 46is a socket. 47If it is of type 48.Dv SOCK_DGRAM , 49this call specifies the peer with which the socket is to be associated; 50this address is that to which datagrams are to be sent, 51and the only address from which datagrams are to be received. 52If the socket is of type 53.Dv SOCK_STREAM , 54this call attempts to make a connection to 55another socket. 56The other socket is specified by 57.Fa name , 58which is an address in the communications space of the socket. 59.Fa namelen 60indicates the amount of space pointed to by 61.Fa name , 62in bytes. 63Each communications space interprets the 64.Fa name 65parameter in its own way. 66Generally, stream sockets may use 67.Fn connect 68only once; datagram sockets may use 69.Fn connect 70multiple times to change their association. 71Datagram sockets may dissolve the association 72by connecting to an invalid address, such as a null address. 73.Sh RETURN VALUES 74If the connection or binding succeeds, 0 is returned. 75Otherwise a \-1 is returned, and a more specific error 76code is stored in 77.Va errno . 78.Sh ERRORS 79The 80.Fn connect 81call fails if: 82.Bl -tag -width Er 83.It Bq Er EBADF 84.Fa s 85is not a valid descriptor. 86.It Bq Er ENOTSOCK 87.Fa s 88is not a socket. 89.It Bq Er EADDRNOTAVAIL 90The specified address is not available on this machine. 91.It Bq Er EAFNOSUPPORT 92Addresses in the specified address family cannot be used with this socket. 93.It Bq Er EISCONN 94The socket is already connected. 95.It Bq Er ETIMEDOUT 96Connection establishment timed out without establishing a connection. 97.It Bq Er EINVAL 98A TCP connection with a local broadcast, the all-ones or a 99multicast address as the peer was attempted. 100.It Bq Er ECONNREFUSED 101The attempt to connect was forcefully rejected. 102.It Bq Er EHOSTUNREACH 103The destination address specified an unreachable host. 104.It Bq Er EINTR 105A connect was interrupted before it succeeded 106by the delivery of a signal. 107.It Bq Er ENETUNREACH 108The network isn't reachable from this host. 109.It Bq Er EADDRINUSE 110The address is already in use. 111.It Bq Er EFAULT 112The 113.Fa name 114parameter specifies an area outside 115the process address space. 116.It Bq Er EINPROGRESS 117The socket is non-blocking 118and the connection cannot 119be completed immediately. 120It is possible to 121.Xr select 2 122or 123.Xr poll 2 124for completion by selecting the socket for writing, and also use 125.Xr getsockopt 2 126with 127.Dv SO_ERROR 128to check for error conditions. 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 Ns -domain . 137These errors may not apply in future versions of the 138.Ux 139IPC domain. 140.Bl -tag -width Er 141.It Bq Er ENOTDIR 142A component of the path prefix is not a directory. 143.It Bq Er ENAMETOOLONG 144A component of a pathname exceeded 145.Dv NAME_MAX 146characters, or an entire pathname (including the terminating NUL) 147exceeded 148.Dv PATH_MAX 149bytes. 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 STANDARDS 180The 181.Fn connect 182function conforms to 183.St -p1003.1-2008 . 184.Sh HISTORY 185The 186.Fn connect 187system call first appeared in 188.Bx 4.1c . 189