361 lines
8.0 KiB
HTML
361 lines
8.0 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of CONNECT</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>CONNECT</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
connect - initiate a connection on a socket
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B> /* See NOTES */
|
|
<BR>
|
|
<B>#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>></B>
|
|
|
|
<B>int connect(int </B><I>sockfd</I><B>, const struct sockaddr *</B><I>addr</I><B>,</B>
|
|
<B> socklen_t </B><I>addrlen</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>connect</B>()
|
|
|
|
system call connects the socket referred to by the file descriptor
|
|
<I>sockfd</I>
|
|
|
|
to the address specified by
|
|
<I>addr</I>.
|
|
|
|
The
|
|
<I>addrlen</I>
|
|
|
|
argument specifies the size of
|
|
<I>addr</I>.
|
|
|
|
The format of the address in
|
|
<I>addr</I>
|
|
|
|
is determined by the address space of the socket
|
|
<I>sockfd</I>;
|
|
|
|
see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2)
|
|
|
|
for further details.
|
|
<P>
|
|
|
|
If the socket
|
|
<I>sockfd</I>
|
|
|
|
is of type
|
|
<B>SOCK_DGRAM</B>,
|
|
|
|
then
|
|
<I>addr</I>
|
|
|
|
is the address to which datagrams are sent by default, and the only
|
|
address from which datagrams are received.
|
|
If the socket is of type
|
|
<B>SOCK_STREAM</B>
|
|
|
|
or
|
|
<B>SOCK_SEQPACKET</B>,
|
|
|
|
this call attempts to make a connection to the socket that is bound
|
|
to the address specified by
|
|
<I>addr</I>.
|
|
|
|
<P>
|
|
|
|
Generally, connection-based protocol sockets may successfully
|
|
<B>connect</B>()
|
|
|
|
only once; connectionless protocol sockets may use
|
|
<B>connect</B>()
|
|
|
|
multiple times to change their association.
|
|
Connectionless sockets may
|
|
dissolve the association by connecting to an address with the
|
|
<I>sa_family</I>
|
|
|
|
member of
|
|
<I>sockaddr</I>
|
|
|
|
set to
|
|
<B>AF_UNSPEC</B>
|
|
|
|
(supported on Linux since kernel 2.2).
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
If the connection or binding succeeds, zero is returned.
|
|
On error, -1 is returned, and
|
|
<I>errno</I>
|
|
|
|
is set appropriately.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
The following are general socket errors only.
|
|
There may be other domain-specific error codes.
|
|
<DL COMPACT>
|
|
<DT id="1"><B>EACCES</B>
|
|
|
|
<DD>
|
|
For UNIX domain sockets, which are identified by pathname:
|
|
Write permission is denied on the socket file,
|
|
or search permission is denied for one of the directories
|
|
in the path prefix.
|
|
(See also
|
|
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7).)
|
|
|
|
<DT id="2"><B>EACCES</B>, <B>EPERM</B>
|
|
|
|
<DD>
|
|
The user tried to connect to a broadcast address without having the socket
|
|
broadcast flag enabled or the connection request failed because of a local
|
|
firewall rule.
|
|
<DT id="3"><B>EADDRINUSE</B>
|
|
|
|
<DD>
|
|
Local address is already in use.
|
|
<DT id="4"><B>EADDRNOTAVAIL</B>
|
|
|
|
<DD>
|
|
(Internet domain sockets)
|
|
The socket referred to by
|
|
<I>sockfd</I>
|
|
|
|
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</I>
|
|
|
|
in
|
|
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7).
|
|
|
|
<DT id="5"><B>EAFNOSUPPORT</B>
|
|
|
|
<DD>
|
|
The passed address didn't have the correct address family in its
|
|
<I>sa_family</I>
|
|
|
|
field.
|
|
<DT id="6"><B>EAGAIN</B>
|
|
|
|
<DD>
|
|
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.
|
|
<DT id="7"><B>EALREADY</B>
|
|
|
|
<DD>
|
|
The socket is nonblocking and a previous connection attempt has not yet
|
|
been completed.
|
|
<DT id="8"><B>EBADF</B>
|
|
|
|
<DD>
|
|
<I>sockfd</I>
|
|
|
|
is not a valid open file descriptor.
|
|
<DT id="9"><B>ECONNREFUSED</B>
|
|
|
|
<DD>
|
|
A
|
|
<B>connect</B>()
|
|
|
|
on a stream socket found no one listening on the remote address.
|
|
<DT id="10"><B>EFAULT</B>
|
|
|
|
<DD>
|
|
The socket structure address is outside the user's address space.
|
|
<DT id="11"><B>EINPROGRESS</B>
|
|
|
|
<DD>
|
|
The socket is nonblocking and the connection cannot be completed immediately.
|
|
(UNIX domain sockets failed with
|
|
<B>EAGAIN</B>
|
|
|
|
instead.)
|
|
It is possible to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2)
|
|
|
|
for completion by selecting the socket for writing.
|
|
After
|
|
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2)
|
|
|
|
indicates writability, use
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2)
|
|
|
|
to read the
|
|
<B>SO_ERROR</B>
|
|
|
|
option at level
|
|
<B>SOL_SOCKET</B>
|
|
|
|
to determine whether
|
|
<B>connect</B>()
|
|
|
|
completed successfully
|
|
(<B>SO_ERROR</B>
|
|
|
|
is zero) or unsuccessfully
|
|
(<B>SO_ERROR</B>
|
|
|
|
is one of the usual error codes listed here,
|
|
explaining the reason for the failure).
|
|
<DT id="12"><B>EINTR</B>
|
|
|
|
<DD>
|
|
The system call was interrupted by a signal that was caught; see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
|
|
|
|
|
|
|
|
<DT id="13"><B>EISCONN</B>
|
|
|
|
<DD>
|
|
The socket is already connected.
|
|
<DT id="14"><B>ENETUNREACH</B>
|
|
|
|
<DD>
|
|
Network is unreachable.
|
|
<DT id="15"><B>ENOTSOCK</B>
|
|
|
|
<DD>
|
|
The file descriptor
|
|
<I>sockfd</I>
|
|
|
|
does not refer to a socket.
|
|
<DT id="16"><B>EPROTOTYPE</B>
|
|
|
|
<DD>
|
|
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.
|
|
<DT id="17"><B>ETIMEDOUT</B>
|
|
|
|
<DD>
|
|
Timeout while attempting connection.
|
|
The server may be too
|
|
busy to accept new connections.
|
|
Note that for IP sockets the timeout may
|
|
be very long when syncookies are enabled on the server.
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD,
|
|
(connect<B>()</B>
|
|
|
|
first appeared in 4.2BSD).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
POSIX.1 does not require the inclusion of
|
|
<I><<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></I>,
|
|
|
|
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.
|
|
<P>
|
|
|
|
For background on the
|
|
<I>socklen_t</I>
|
|
|
|
type, see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2).
|
|
|
|
<P>
|
|
|
|
If
|
|
<B>connect</B>()
|
|
|
|
fails, consider the state of the socket as unspecified.
|
|
Portable applications should close the socket and create a new one for
|
|
reconnecting.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
An example of the use of
|
|
<B>connect</B>()
|
|
|
|
is shown in
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3).
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getsockname">getsockname</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+listen">listen</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7)
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="18"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="19"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="20"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="21"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="22"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="23"><A HREF="#lbAG">CONFORMING TO</A><DD>
|
|
<DT id="24"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DT id="25"><A HREF="#lbAI">EXAMPLE</A><DD>
|
|
<DT id="26"><A HREF="#lbAJ">SEE ALSO</A><DD>
|
|
<DT id="27"><A HREF="#lbAK">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:32 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|