1LISTEN(2)                  Linux Programmer's Manual                 LISTEN(2)
2
3
4

NAME

6       listen - listen for connections on a socket
7

SYNOPSIS

9       #include <sys/types.h>          /* See NOTES */
10       #include <sys/socket.h>
11
12       int listen(int sockfd, int backlog);
13

DESCRIPTION

15       listen()  marks  the  socket referred to by sockfd as a passive socket,
16       that is, as a socket that will be used to  accept  incoming  connection
17       requests using accept(2).
18
19       The  sockfd  argument  is  a file descriptor that refers to a socket of
20       type SOCK_STREAM or SOCK_SEQPACKET.
21
22       The backlog argument defines the maximum length to which the  queue  of
23       pending  connections  for  sockfd  may  grow.   If a connection request
24       arrives when the queue is full, the client may receive an error with an
25       indication  of  ECONNREFUSED  or,  if  the underlying protocol supports
26       retransmission, the request may be ignored so that a later reattempt at
27       connection succeeds.
28

RETURN VALUE

30       On  success,  zero is returned.  On error, -1 is returned, and errno is
31       set appropriately.
32

ERRORS

34       EADDRINUSE
35              Another socket is already listening on the same port.
36
37       EBADF  The argument sockfd is not a valid descriptor.
38
39       ENOTSOCK
40              The argument sockfd is not a socket.
41
42       EOPNOTSUPP
43              The socket is not of a type that supports  the  listen()  opera‐
44              tion.
45

CONFORMING TO

47       4.4BSD,  POSIX.1-2001.   The  listen()  function call first appeared in
48       4.2BSD.
49

NOTES

51       To accept connections, the following steps are performed:
52
53           1.  A socket is created with socket(2).
54
55           2.  The socket is bound to a local address using bind(2),  so  that
56               other sockets may be connect(2)ed to it.
57
58           3.  A  willingness to accept incoming connections and a queue limit
59               for incoming connections are specified with listen().
60
61           4.  Connections are accepted with accept(2).
62
63       POSIX.1-2001 does not require the inclusion of <sys/types.h>, and  this
64       header  file  is not required on Linux.  However, some historical (BSD)
65       implementations required this header file,  and  portable  applications
66       are probably wise to include it.
67
68       The  behavior of the backlog argument on TCP sockets changed with Linux
69       2.2.  Now it specifies the  queue  length  for  completely  established
70       sockets  waiting  to  be  accepted, instead of the number of incomplete
71       connection requests.  The maximum length of the  queue  for  incomplete
72       sockets  can be set using /proc/sys/net/ipv4/tcp_max_syn_backlog.  When
73       syncookies are enabled there is no logical maximum length and this set‐
74       ting is ignored.  See tcp(7) for more information.
75
76       If    the   backlog   argument   is   greater   than   the   value   in
77       /proc/sys/net/core/somaxconn, then it is  silently  truncated  to  that
78       value;  the  default  value  in  this  file  is 128.  In kernels before
79       2.4.25, this limit was a hard coded value, SOMAXCONN,  with  the  value
80       128.
81

EXAMPLE

83       See bind(2).
84

SEE ALSO

86       accept(2), bind(2), connect(2), socket(2), socket(7)
87

COLOPHON

89       This  page  is  part of release 3.25 of the Linux man-pages project.  A
90       description of the project, and information about reporting  bugs,  can
91       be found at http://www.kernel.org/doc/man-pages/.
92
93
94
95Linux                             2008-11-20                         LISTEN(2)
Impressum