INtime SDK Help

Listen for connections on a socket.

#include <sys/types.h>
#include <sys/socket.h>

listen(int s, int backlog);


To accept connections, a socket is first created with socket, a willingness to accept incoming connections and a queue limit for incoming connections are specified with listen(), and then the connections are accepted with accept. The listen() system call applies only to sockets of type SOCK_STREAM or SOCK_SEQPACKET.

The backlog argument defines the maximum length the queue of pending connections may grow to. The real maximum queue length will be 1.5 times more than the value specified in the backlog argument. A subsequent listen() system call on the listening socket allows the caller to change the maximum queue length using a new backlog argument. If a connection request arrives with the queue full the client may receive an error with an indication of ECONNREFUSED, or, in the case of TCP, the connection will be silently dropped.

Current queue lengths of listening sockets can be queried using netstat command.

Note that before FreeBSD 4.5 and the introduction of the syncache, the backlog argument also determined the length of the incomplete connection queue, which held TCP sockets in the process of completing TCP's 3-way handshake. These incomplete connections are now held entirely in the syncache, which is unaffected by queue lengths. Inflated backlog values to help handle denial of service attacks are no longer necessary.

The sysctl MIB variable kern.ipc.somaxconn specifies a hard limit on backlog; if a value greater than kern.ipc.somaxconn or less than zero is specified, backlog is silently forced to kern.ipc.somaxconn.


Note: Not supported by INtime.

When accept filtering is used on a socket, a second queue will be used to hold sockets that have connected, but have not yet met their accept filtering criteria. Once the criteria has been met, these sockets will be moved over into the completed connection queue to be accepted. If this secondary queue is full and a new connection comes in, the oldest socket which has not yet met its accept filter criteria will be terminated.

This secondary queue, like the primary listen queue, is sized according to the backlog argument.

Return Values

The listen() function returns the value 0 if successful; otherwisethe value -1 is returned and the global variable errno is set to indicate the error.

The listen() system call will fail if:

EBADF The argument s is not a valid descriptor.
EINVAL The socket is already connected, or in the process of being connected.
ENOTSOCK The argument s is not a socket.
EOPNOTSUPP The socket is not of a type that supports the operation listen().


Versions Link to
INtime 4.0 netlib.lib
See Also