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