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.\" @(#)connect.2 8.1 (Berkeley) 6/4/93 29.\" 30.Dd August 18, 2016 31.Dt CONNECT 2 32.Os 33.Sh NAME 34.Nm connect 35.Nd initiate a connection on a socket 36.Sh LIBRARY 37.Lb libc 38.Sh SYNOPSIS 39.In sys/socket.h 40.Ft int 41.Fn connect "int s" "const struct sockaddr *name" "socklen_t namelen" 42.Sh DESCRIPTION 43The 44.Fa s 45argument 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 69argument in its own way. 70Generally, stream sockets may successfully 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 78.Rv -std connect 79.Sh ERRORS 80The 81.Fn connect 82system call fails if: 83.Bl -tag -width Er 84.It Bq Er EBADF 85The 86.Fa s 87argument 88is not a valid descriptor. 89.It Bq Er EINVAL 90The 91.Fa namelen 92argument is not a valid length for the address family. 93.It Bq Er ENOTSOCK 94The 95.Fa s 96argument 97is a descriptor for a file, not a socket. 98.It Bq Er EADDRNOTAVAIL 99The specified address is not available on this machine. 100.It Bq Er EAFNOSUPPORT 101Addresses in the specified address family cannot be used with this socket. 102.It Bq Er EISCONN 103The socket is already connected. 104.It Bq Er ETIMEDOUT 105Connection establishment timed out without establishing a connection. 106.It Bq Er ECONNREFUSED 107The attempt to connect was forcefully rejected. 108.It Bq Er ECONNRESET 109The connection was reset by the remote host. 110.It Bq Er ENETUNREACH 111The network is not reachable from this host. 112.It Bq Er EHOSTUNREACH 113The remote host is not reachable from this host. 114.It Bq Er EADDRINUSE 115The address is already in use. 116.It Bq Er EFAULT 117The 118.Fa name 119argument specifies an area outside 120the process address space. 121.It Bq Er EINPROGRESS 122The socket is non-blocking 123and the connection cannot 124be completed immediately. 125It is possible to 126.Xr select 2 127for completion by selecting the socket for writing. 128.It Bq Er EINTR 129The connection attempt was interrupted by the delivery of a signal. 130The connection will be established in the background, 131as in the case of 132.Er EINPROGRESS . 133.It Bq Er EALREADY 134A previous connection attempt has not yet been completed. 135.It Bq Er EACCES 136An attempt is made to connect to a broadcast address (obtained through the 137.Dv INADDR_BROADCAST 138constant or the 139.Dv INADDR_NONE 140return value) through a socket that does not provide broadcast functionality. 141.It Bq Er EAGAIN 142An auto-assigned port number was requested but no auto-assigned ports 143are available. 144Increasing the port range specified by 145.Xr sysctl 3 146MIB variables 147.Va net.inet.ip.portrange.first 148and 149.Va net.inet.ip.portrange.last 150may alleviate the problem. 151.El 152.Pp 153The following errors are specific to connecting names in the UNIX domain. 154These errors may not apply in future versions of the UNIX IPC domain. 155.Bl -tag -width Er 156.It Bq Er ENOTDIR 157A component of the path prefix is not a directory. 158.It Bq Er ENAMETOOLONG 159A component of a pathname exceeded 255 characters, 160or an entire path name exceeded 1023 characters. 161.It Bq Er ENOENT 162The named socket does not exist. 163.It Bq Er EACCES 164Search permission is denied for a component of the path prefix. 165.It Bq Er EACCES 166Write access to the named socket is denied. 167.It Bq Er ELOOP 168Too many symbolic links were encountered in translating the pathname. 169.It Bq Er EPERM 170Write access to the named socket is denied. 171.El 172.Sh SEE ALSO 173.Xr accept 2 , 174.Xr getpeername 2 , 175.Xr getsockname 2 , 176.Xr select 2 , 177.Xr socket 2 , 178.Xr sysctl 3 , 179.Xr sysctl 8 180.Sh HISTORY 181The 182.Fn connect 183system call appeared in 184.Bx 4.2 . 185