man-pages/man2/listen.2.html
2021-03-31 01:06:50 +01:00

255 lines
5.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of LISTEN</TITLE>
</HEAD><BODY>
<H1>LISTEN</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2020-02-09<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
listen - listen for connections on a socket
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</B> /* See NOTES */
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<B>int listen(int </B><I>sockfd</I><B>, int </B><I>backlog</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>listen</B>()
marks the socket referred to by
<I>sockfd</I>
as a passive socket, that is, as a socket that will
be used to accept incoming connection requests using
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2).
<P>
The
<I>sockfd</I>
argument is a file descriptor that refers to a socket of type
<B>SOCK_STREAM</B>
or
<B>SOCK_SEQPACKET</B>.
<P>
The
<I>backlog</I>
argument defines the maximum length
to which the queue of pending connections for
<I>sockfd</I>
may grow.
If a connection request arrives when the queue is full, the client
may receive an error with an indication of
<B>ECONNREFUSED</B>
or, if the underlying protocol supports retransmission, the request may be
ignored so that a later reattempt at connection succeeds.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EADDRINUSE</B>
<DD>
Another socket is already listening on the same port.
<DT id="2"><B>EADDRINUSE</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="3"><B>EBADF</B>
<DD>
The argument
<I>sockfd</I>
is not a valid file descriptor.
<DT id="4"><B>ENOTSOCK</B>
<DD>
The file descriptor
<I>sockfd</I>
does not refer to a socket.
<DT id="5"><B>EOPNOTSUPP</B>
<DD>
The socket is not of a type that supports the
<B>listen</B>()
operation.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, 4.4BSD
(<B>listen</B>()
first appeared in 4.2BSD).
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
To accept connections, the following steps are performed:
<DL COMPACT><DT id="6"><DD>
<DL COMPACT>
<DT id="7">1.<DD>
A socket is created with
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2).
<DT id="8">2.<DD>
The socket is bound to a local address using
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2),
so that other sockets may be
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)ed
to it.
<DT id="9">3.<DD>
A willingness to accept incoming connections and a queue limit for incoming
connections are specified with
<B>listen</B>().
<DT id="10">4.<DD>
Connections are accepted with
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2).
</DL>
</DL>
<P>
POSIX.1 does not require the inclusion of
<I>&lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</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>
The behavior of the
<I>backlog</I>
argument on TCP sockets changed with Linux 2.2.
Now it specifies the queue length for
<I>completely</I>
established sockets waiting to be accepted,
instead of the number of incomplete connection requests.
The maximum length of the queue for incomplete sockets
can be set using
<I>/proc/sys/net/ipv4/tcp_max_syn_backlog</I>.
When syncookies are enabled there is no logical maximum
length and this setting is ignored.
See
<B><A HREF="/cgi-bin/man/man2html?7+tcp">tcp</A></B>(7)
for more information.
<P>
If the
<I>backlog</I>
argument is greater than the value in
<I>/proc/sys/net/core/somaxconn</I>,
then it is silently truncated to that value.
Since Linux 5.4, the default in this file is 4096;
in earlier kernels, the default value is 128.
In kernels before 2.4.25, this limit was a hard coded value,
<B>SOMAXCONN</B>,
with the value 128.
<A NAME="lbAI">&nbsp;</A>
<H2>EXAMPLE</H2>
See
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2).
<A NAME="lbAJ">&nbsp;</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+connect">connect</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7)
<A NAME="lbAK">&nbsp;</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">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="11"><A HREF="#lbAB">NAME</A><DD>
<DT id="12"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="13"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="14"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="15"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="16"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="17"><A HREF="#lbAH">NOTES</A><DD>
<DT id="18"><A HREF="#lbAI">EXAMPLE</A><DD>
<DT id="19"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="20"><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:33 GMT, March 31, 2021
</BODY>
</HTML>