1.\" $NetBSD: connect.2,v 1.8 1995/10/12 15:40:48 jtc Exp $ 2.\" 3.\" Copyright (c) 1983, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)connect.2 8.1 (Berkeley) 6/4/93 35.\" 36.Dd March 18, 2015 37.Dt CONNECT 2 38.Os BSD 4.2 39.Sh NAME 40.Nm connect 41.Nd initiate a connection on a socket 42.Sh SYNOPSIS 43.Fd #include <sys/types.h> 44.Fd #include <sys/socket.h> 45.Ft int 46.Fo connect 47.Fa "int socket" 48.Fa "const struct sockaddr *address" 49.Fa "socklen_t address_len" 50.Fc 51.Sh DESCRIPTION 52The parameter 53.Fa socket 54is a socket. 55If it is of type 56.Dv SOCK_DGRAM , 57this call specifies the peer with which the socket is to be associated; 58this address is that to which datagrams are to be sent, 59and the only address from which datagrams are to be received. 60If the socket is of type 61.Dv SOCK_STREAM , 62this call attempts to make a connection to 63another socket. 64The other socket is specified by 65.Fa address , 66which is an address in the communications space of the socket. 67.Pp 68Each communications space interprets the 69.Fa address 70parameter 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 calling 78.Xr disconnectx 2 , 79or by connecting to an invalid address, such as a null address 80or an address with 81the address family set to 82.Dv AF_UNSPEC 83(the error 84.Dv EAFNOSUPPORT 85will be harmlessly returned). 86.Sh RETURN VALUES 87Upon successful completion, a value of 0 is returned. 88Otherwise, a value of -1 is returned and the global integer variable 89.Va errno 90is set to indicate the error. 91.Sh ERRORS 92The 93.Fn connect 94system call will fail if: 95.Bl -tag -width Er 96.\" ========== 97.It Bq Er EACCES 98The destination address is a broadcast address and the 99socket option 100.Dv SO_BROADCAST 101is not set. 102.\" ========== 103.It Bq Er EADDRINUSE 104The address is already in use. 105.\" ========== 106.It Bq Er EADDRNOTAVAIL 107The specified address is not available on this machine. 108.\" ========== 109.It Bq Er EAFNOSUPPORT 110Addresses in the specified address family cannot be used with this socket. 111.\" ========== 112.It Bq Er EALREADY 113The socket is non-blocking 114and a previous connection attempt 115has not yet been completed. 116.\" ========== 117.It Bq Er EBADF 118.Fa socket 119is not a valid descriptor. 120.\" ========== 121.It Bq Er ECONNREFUSED 122The attempt to connect was ignored 123(because the target is not listening for connections) 124or explicitly rejected. 125.\" ========== 126.It Bq Er EFAULT 127The 128.Fa address 129parameter specifies an area outside 130the process address space. 131.\" ========== 132.It Bq Er EHOSTUNREACH 133The target host cannot be reached (e.g., down, disconnected). 134.\" ========== 135.It Bq Er EINPROGRESS 136The socket is non-blocking 137and the connection cannot 138be completed immediately. 139It is possible to 140.Xr select 2 141for completion by selecting the socket for writing. 142.\" ========== 143.It Bq Er EINTR 144Its execution was interrupted by a signal. 145.\" ========== 146.It Bq Er EINVAL 147An invalid argument was detected 148(e.g., 149.Fa address_len 150is not valid for the address family, 151the specified address family is invalid). 152.\" ========== 153.It Bq Er EISCONN 154The socket is already connected. 155.\" ========== 156.It Bq Er ENETDOWN 157The local network interface is not functioning. 158.\" ========== 159.It Bq Er ENETUNREACH 160The network isn't reachable from this host. 161.\" ========== 162.It Bq Er ENOBUFS 163The system call was unable to allocate a needed memory buffer. 164.\" ========== 165.It Bq Er ENOTSOCK 166.Fa socket 167is not a file descriptor for a socket. 168.\" ========== 169.It Bq Er EOPNOTSUPP 170Because 171.Fa socket 172is listening, no connection is allowed. 173.\" ========== 174.It Bq Er EPROTOTYPE 175.Fa address 176has a different type than the socket 177that is bound to the specified peer address. 178.\" ========== 179.It Bq Er ETIMEDOUT 180Connection establishment timed out without establishing a connection. 181.\" ========== 182.It Bq Er ECONNRESET 183Remote host reset the connection request. 184.El 185.Pp 186The following errors are specific to connecting names in the UNIX domain. 187These errors may not apply in future versions of the UNIX IPC domain. 188.Bl -tag -width Er 189.\" ========== 190.It Bq Er EACCES 191Search permission is denied for a component of the path prefix. 192.\" ========== 193.It Bq Er EACCES 194Write access to the named socket is denied. 195.\" ========== 196.It Bq Er EIO 197An I/O error occurred while reading from or writing to the file system. 198.\" ========== 199.It Bq Er ELOOP 200Too many symbolic links were encountered in translating the pathname. 201This is taken to be indicative of a looping symbolic link. 202.\" ========== 203.It Bq Er ENAMETOOLONG 204A component of a pathname exceeded 205.Dv {NAME_MAX} 206characters, or an entire path name exceeded 207.Dv {PATH_MAX} 208characters. 209.\" ========== 210.It Bq Er ENOENT 211The named socket does not exist. 212.\" ========== 213.It Bq Er ENOTDIR 214A component of the path prefix is not a directory. 215.El 216.Sh LEGACY SYNOPSIS 217.Fd #include <sys/types.h> 218.Fd #include <sys/socket.h> 219.Pp 220The include file 221.In sys/types.h 222is necessary. 223.Sh SEE ALSO 224.Xr accept 2 , 225.Xr connectx 2 , 226.Xr disconnectx 2 , 227.Xr getsockname 2 , 228.Xr select 2 , 229.Xr socket 2 , 230.Xr compat 5 231.Sh HISTORY 232The 233.Fn connect 234function call appeared in 235.Bx 4.2 . 236