1.\" $OpenBSD: connect.2,v 1.27 2015/10/11 07:25:11 guenther 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: October 11 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.In 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; the 63.Fa sa_len 64member of 65.Fa name 66is ignored. 67Each communications space interprets the 68.Fa name 69parameter in its own way. 70Generally, stream sockets may use 71.Fn connect 72only once; datagram sockets may use 73.Fn connect 74multiple times to change their association. 75Datagram sockets may dissolve the association 76by connecting to an invalid address, such as a null address. 77.Sh RETURN VALUES 78If the connection or binding succeeds, 0 is returned. 79Otherwise a \-1 is returned, and a more specific error 80code is stored in 81.Va errno . 82.Sh ERRORS 83The 84.Fn connect 85call fails if: 86.Bl -tag -width Er 87.It Bq Er EBADF 88.Fa s 89is not a valid descriptor. 90.It Bq Er ENOTSOCK 91.Fa s 92is not a socket. 93.It Bq Er EADDRNOTAVAIL 94The specified address is not available on this machine. 95.It Bq Er EAFNOSUPPORT 96Addresses in the specified address family cannot be used with this socket. 97.It Bq Er EISCONN 98The socket is already connected. 99.It Bq Er ETIMEDOUT 100Connection establishment timed out without establishing a connection. 101.It Bq Er EINVAL 102A TCP connection with a local broadcast, the all-ones or a 103multicast address as the peer was attempted. 104.It Bq Er ECONNREFUSED 105The attempt to connect was forcefully rejected. 106.It Bq Er EHOSTUNREACH 107The destination address specified an unreachable host. 108.It Bq Er EINTR 109A connect was interrupted before it succeeded 110by the delivery of a signal. 111.It Bq Er ENETUNREACH 112The network isn't reachable from this host. 113.It Bq Er EADDRINUSE 114The address is already in use. 115.It Bq Er EFAULT 116The 117.Fa name 118parameter specifies an area outside 119the process address space. 120.It Bq Er EINPROGRESS 121The socket is non-blocking 122and the connection cannot 123be completed immediately. 124It is possible to 125.Xr select 2 126or 127.Xr poll 2 128for completion by selecting the socket for writing, and also use 129.Xr getsockopt 2 130with 131.Dv SO_ERROR 132to check for error conditions. 133.It Bq Er EALREADY 134The socket is non-blocking 135and a previous connection attempt 136has not yet been completed. 137.El 138.Pp 139The following errors are specific to connecting names in the 140.Ux Ns -domain . 141These errors may not apply in future versions of the 142.Ux 143IPC domain. 144.Bl -tag -width Er 145.It Bq Er ENOTDIR 146A component of the path prefix is not a directory. 147.It Bq Er ENAMETOOLONG 148A component of a pathname exceeded 149.Dv NAME_MAX 150characters, or an entire pathname (including the terminating NUL) 151exceeded 152.Dv PATH_MAX 153bytes. 154.It Bq Er ENOENT 155The named socket does not exist. 156.It Bq Er EACCES 157Search permission is denied for a component of the path prefix. 158.It Bq Er EACCES 159Write access to the named socket is denied. 160.It Bq Er ELOOP 161Too many symbolic links were encountered in translating the pathname. 162.It Bq Er EPROTOTYPE 163The file described by 164.Fa name 165is of a different type than 166.Fa s . 167E.g., 168.Fa s 169may be of type 170.Dv SOCK_STREAM 171whereas 172.Fa name 173may refer to a socket of type 174.Dv SOCK_DGRAM . 175.El 176.Sh SEE ALSO 177.Xr accept 2 , 178.Xr getsockname 2 , 179.Xr getsockopt 2 , 180.Xr poll 2 , 181.Xr select 2 , 182.Xr socket 2 183.Sh STANDARDS 184The 185.Fn connect 186function conforms to 187.St -p1003.1-2008 . 188.Sh HISTORY 189The 190.Fn connect 191system call first appeared in 192.Bx 4.1c . 193