1.\" $OpenBSD: connect.2,v 1.31 2016/08/20 20:22:28 millert 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: August 20 2016 $ 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.Pp 78If the socket is in non-blocking mode and the connection cannot be 79completed immediately, or if it is interrupted by a signal, 80.Fn connect 81will return an error and the connection attempt will proceed 82asynchronously. 83Subsequent calls to 84.Fn connect 85will fail with errno set to 86.Er EALREADY . 87It is possible to use 88.Xr select 2 89or 90.Xr poll 2 91to determine when the connect operation has completed by checking the 92socket for writability. 93The success or failure of the connection attempt may be determined by using 94.Xr getsockopt 2 95to check the socket error status with the 96.Dv SO_ERROR 97option at the 98.Dv SOL_SOCKET 99level. 100If the connection was successful, the error value will be zero. 101Otherwise, it will be one of the error values listed below. 102.Sh RETURN VALUES 103If the connection or binding succeeds, 0 is returned. 104Otherwise a \-1 is returned, and a more specific error 105code is stored in 106.Va errno . 107.Sh EXAMPLES 108The following code connects to the host described by 109.Fa name 110and handles the case where 111.Fn connect 112is interrupted by a signal. 113.Bd -literal -offset indent 114#include <sys/socket.h> 115#include <poll.h> 116#include <errno.h> 117#include <err.h> 118 119int 120connect_wait(int s) 121{ 122 struct pollfd pfd[1]; 123 int error = 0; 124 socklen_t len = sizeof(error); 125 126 pfd[0].fd = s; 127 pfd[0].events = POLLOUT; 128 129 if (poll(pfd, 1, -1) == -1) 130 return -1; 131 if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &len) < 0) 132 return -1; 133 if (error != 0) { 134 errno = error; 135 return -1; 136 } 137 return 0; 138} 139 140\&... 141 142int retcode; 143 144\&... 145 146for (retcode = connect(s, name, namelen); 147 retcode != 0 && errno == EINTR; 148 retcode = connect_wait(s)) 149 continue; 150if (retcode == -1) 151 err(1, "connect"); 152.Ed 153.Sh ERRORS 154The 155.Fn connect 156call fails if: 157.Bl -tag -width Er 158.It Bq Er EBADF 159.Fa s 160is not a valid descriptor. 161.It Bq Er ENOTSOCK 162.Fa s 163is not a socket. 164.It Bq Er EADDRNOTAVAIL 165The specified address is not available on this machine. 166.It Bq Er EAFNOSUPPORT 167Addresses in the specified address family cannot be used with this socket. 168.It Bq Er EISCONN 169The socket is already connected. 170.It Bq Er ETIMEDOUT 171Connection establishment timed out without establishing a connection. 172.It Bq Er EINVAL 173A TCP connection with a local broadcast, the all-ones or a 174multicast address as the peer was attempted. 175.It Bq Er ECONNREFUSED 176The attempt to connect was forcefully rejected. 177.It Bq Er EHOSTUNREACH 178The destination address specified an unreachable host. 179.It Bq Er EINTR 180The connection attempt was interrupted by a signal. 181The attempt will continue asynchronously as if the socket was non-blocking. 182.It Bq Er ENETUNREACH 183The network isn't reachable from this host. 184.It Bq Er EADDRINUSE 185The address is already in use. 186.It Bq Er EFAULT 187The 188.Fa name 189parameter specifies an area outside 190the process address space. 191.It Bq Er EINPROGRESS 192The socket is non-blocking and the connection cannot 193be completed immediately. 194.It Bq Er EALREADY 195Either the socket is non-blocking or a previous call to 196.Fn connect 197was interrupted by a signal, and the connection attempt has not yet 198been completed. 199.El 200.Pp 201The following errors are specific to connecting names in the 202.Ux Ns -domain . 203These errors may not apply in future versions of the 204.Ux 205IPC domain. 206.Bl -tag -width Er 207.It Bq Er ENOTDIR 208A component of the path prefix is not a directory. 209.It Bq Er ENAMETOOLONG 210A component of a pathname exceeded 211.Dv NAME_MAX 212characters, or an entire pathname (including the terminating NUL) 213exceeded 214.Dv PATH_MAX 215bytes. 216.It Bq Er ENOENT 217The named socket does not exist. 218.It Bq Er EACCES 219Search permission is denied for a component of the path prefix. 220.It Bq Er EACCES 221Write access to the named socket is denied. 222.It Bq Er ELOOP 223Too many symbolic links were encountered in translating the pathname. 224.It Bq Er EPROTOTYPE 225The file described by 226.Fa name 227is of a different type than 228.Fa s . 229E.g., 230.Fa s 231may be of type 232.Dv SOCK_STREAM 233whereas 234.Fa name 235may refer to a socket of type 236.Dv SOCK_DGRAM . 237.El 238.Sh SEE ALSO 239.Xr accept 2 , 240.Xr getsockname 2 , 241.Xr getsockopt 2 , 242.Xr poll 2 , 243.Xr select 2 , 244.Xr socket 2 245.Sh STANDARDS 246The 247.Fn connect 248function conforms to 249.St -p1003.1-2008 . 250.Sh HISTORY 251The 252.Fn connect 253system call first appeared in 254.Bx 4.1c . 255