.\" Modified 1998, 1999 by Andi Kleen
.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
.\"
-.TH CONNECT 2 2008-12-03 "Linux" "Linux Programmer's Manual"
+.TH CONNECT 2 2019-03-06 "Linux" "Linux Programmer's Manual"
.SH NAME
connect \- initiate a connection on a socket
.SH SYNOPSIS
.BR "#include <sys/types.h>" " /* See NOTES */"
.br
.B #include <sys/socket.h>
-.sp
+.PP
.BI "int connect(int " sockfd ", const struct sockaddr *" addr ,
.BI " socklen_t " addrlen );
.fi
see
.BR socket (2)
for further details.
-
+.PP
If the socket
.I sockfd
is of type
-.B SOCK_DGRAM
+.BR SOCK_DGRAM ,
then
.I addr
is the address to which datagrams are sent by default, and the only
.B EADDRINUSE
Local address is already in use.
.TP
+.B EADDRNOTAVAIL
+(Internet domain sockets)
+The socket referred to by
+.I sockfd
+had not previously been bound to an address and,
+upon attempting to bind it to an ephemeral port,
+it was determined that all port numbers in the ephemeral port range
+are currently in use.
+See the discussion of
+.I /proc/sys/net/ipv4/ip_local_port_range
+in
+.BR ip (7).
+.TP
.B EAFNOSUPPORT
The passed address didn't have the correct address family in its
.I sa_family
field.
.TP
.B EAGAIN
-No more free local ports or insufficient entries in the routing cache.
-For
-.B AF_INET
-see the description of
-.I /proc/sys/net/ipv4/ip_local_port_range
-.BR ip (7)
-for information on how to increase the number of local ports.
+For nonblocking UNIX domain sockets, the socket is nonblocking, and the
+connection cannot be completed immediately.
+For other socket families, there are insufficient entries in the routing cache.
.TP
.B EALREADY
The socket is nonblocking and a previous connection attempt has not yet
been completed.
.TP
.B EBADF
-The file descriptor is not a valid index in the descriptor table.
+.I sockfd
+is not a valid open file descriptor.
.TP
.B ECONNREFUSED
-No-one listening on the remote address.
+A
+.BR connect ()
+on a stream socket found no one listening on the remote address.
.TP
.B EFAULT
The socket structure address is outside the user's address space.
.TP
.B EINPROGRESS
-The socket is nonblocking and the connection cannot be completed
-immediately.
+The socket is nonblocking and the connection cannot be completed immediately.
+(UNIX domain sockets failed with
+.BR EAGAIN
+instead.)
It is possible to
.BR select (2)
or
Network is unreachable.
.TP
.B ENOTSOCK
-The file descriptor is not associated with a socket.
+The file descriptor
+.I sockfd
+does not refer to a socket.
+.TP
+.B EPROTOTYPE
+The socket type does not support the requested communications protocol.
+This error can occur, for example,
+on an attempt to connect a UNIX domain datagram socket to a stream socket.
.TP
.B ETIMEDOUT
Timeout while attempting connection.
Note that for IP sockets the timeout may
be very long when syncookies are enabled on the server.
.SH CONFORMING TO
-SVr4, 4.4BSD, (the
-.BR connect ()
-function first appeared in 4.2BSD), POSIX.1-2001.
+POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD,
+.RB (connect ()
+first appeared in 4.2BSD).
.\" SVr4 documents the additional
.\" general error codes
.\" .BR EADDRNOTAVAIL ,
.\" It also
.\" documents many additional error conditions not described here.
.SH NOTES
-POSIX.1-2001 does not require the inclusion of
+POSIX.1 does not require the inclusion of
.IR <sys/types.h> ,
and this header file is not required on Linux.
However, some historical (BSD) implementations required this header
file, and portable applications are probably wise to include it.
-
-The third argument of
-.BR connect ()
-is in reality an
-.I int
-(and this is what 4.x BSD and libc4 and libc5 have).
-Some POSIX confusion resulted in the present
-.IR socklen_t ,
-also used by glibc.
-See also
+.PP
+For background on the
+.I socklen_t
+type, see
.BR accept (2).
+.PP
+If
+.BR connect ()
+fails, consider the state of the socket as unspecified.
+Portable applications should close the socket and create a new one for
+reconnecting.
.SH EXAMPLE
An example of the use of
.BR connect ()