1.\" Copyright (c) 1983, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd August 18, 2016 29.Dt CONNECT 2 30.Os 31.Sh NAME 32.Nm connect 33.Nd initiate a connection on a socket 34.Sh LIBRARY 35.Lb libc 36.Sh SYNOPSIS 37.In sys/socket.h 38.Ft int 39.Fn connect "int s" "const struct sockaddr *name" "socklen_t namelen" 40.Sh DESCRIPTION 41The 42.Fa s 43argument 44is a socket. 45If it is of type 46.Dv SOCK_DGRAM , 47this call specifies the peer with which the socket is to be associated; 48this address is that to which datagrams are to be sent, 49and the only address from which datagrams are to be received. 50If the socket is of type 51.Dv SOCK_STREAM , 52this call attempts to make a connection to 53another socket. 54The other socket is specified by 55.Fa name , 56which is an address in the communications space of the socket. 57.Fa namelen 58indicates the amount of space pointed to by 59.Fa name , 60in bytes; the 61.Fa sa_len 62member of 63.Fa name 64is ignored. 65Each communications space interprets the 66.Fa name 67argument in its own way. 68Generally, stream sockets may successfully 69.Fn connect 70only once; datagram sockets may use 71.Fn connect 72multiple times to change their association. 73Datagram sockets may dissolve the association 74by connecting to an invalid address, such as a null address. 75.Sh RETURN VALUES 76.Rv -std connect 77.Sh ERRORS 78The 79.Fn connect 80system call fails if: 81.Bl -tag -width Er 82.It Bq Er EBADF 83The 84.Fa s 85argument 86is not a valid descriptor. 87.It Bq Er EINVAL 88The 89.Fa namelen 90argument is not a valid length for the address family. 91.It Bq Er ENOTSOCK 92The 93.Fa s 94argument 95is a descriptor for a file, not a socket. 96.It Bq Er EADDRNOTAVAIL 97The specified address is not available on this machine. 98.It Bq Er EAFNOSUPPORT 99Addresses in the specified address family cannot be used with this socket. 100.It Bq Er EISCONN 101The socket is already connected. 102.It Bq Er ETIMEDOUT 103Connection establishment timed out without establishing a connection. 104.It Bq Er ECONNREFUSED 105The attempt to connect was forcefully rejected. 106.It Bq Er ECONNRESET 107The connection was reset by the remote host. 108.It Bq Er ENETUNREACH 109The network is not reachable from this host. 110.It Bq Er EHOSTUNREACH 111The remote host is not reachable from this host. 112.It Bq Er EADDRINUSE 113The address is already in use. 114.It Bq Er EFAULT 115The 116.Fa name 117argument specifies an area outside 118the process address space. 119.It Bq Er EINPROGRESS 120The socket is non-blocking 121and the connection cannot 122be completed immediately. 123It is possible to 124.Xr select 2 125for completion by selecting the socket for writing. 126.It Bq Er EINTR 127The connection attempt was interrupted by the delivery of a signal. 128The connection will be established in the background, 129as in the case of 130.Er EINPROGRESS . 131.It Bq Er EALREADY 132A previous connection attempt has not yet been completed. 133.It Bq Er EACCES 134An attempt is made to connect to a broadcast address (obtained through the 135.Dv INADDR_BROADCAST 136constant or the 137.Dv INADDR_NONE 138return value) through a socket that does not provide broadcast functionality. 139.It Bq Er EAGAIN 140An auto-assigned port number was requested but no auto-assigned ports 141are available. 142Increasing the port range specified by 143.Xr sysctl 3 144MIB variables 145.Va net.inet.ip.portrange.first 146and 147.Va net.inet.ip.portrange.last 148may alleviate the problem. 149.El 150.Pp 151The following errors are specific to connecting names in the UNIX domain. 152These errors may not apply in future versions of the UNIX IPC domain. 153.Bl -tag -width Er 154.It Bq Er ENOTDIR 155A component of the path prefix is not a directory. 156.It Bq Er ENAMETOOLONG 157A component of a pathname exceeded 255 characters, 158or an entire path name exceeded 1023 characters. 159.It Bq Er ENOENT 160The named socket does not exist. 161.It Bq Er EACCES 162Search permission is denied for a component of the path prefix. 163.It Bq Er EACCES 164Write access to the named socket is denied. 165.It Bq Er ELOOP 166Too many symbolic links were encountered in translating the pathname. 167.It Bq Er EPERM 168Write access to the named socket is denied. 169.El 170.Sh SEE ALSO 171.Xr accept 2 , 172.Xr getpeername 2 , 173.Xr getsockname 2 , 174.Xr select 2 , 175.Xr socket 2 , 176.Xr sysctl 3 , 177.Xr sysctl 8 178.Sh HISTORY 179The 180.Fn connect 181system call appeared in 182.Bx 4.2 . 183