linux网络编程之listen函数

NAME

    listen - listen for socket connections and limit the queue of incoming connections

SYNOPSIS

    #include <sys/socket.h>

    int listen(int socket, int backlog);


DESCRIPTION

    The listen() function shall mark a connection-mode socket, specified by the socket argument, as accepting connections.

    The backlog argument provides a hint to the implementation which the implementation shall use to limit the number of outstanding connections in the socket's listen queue. Implementations may impose a limit on backlog and silently reduce the specified value. Normally, a larger backlog argument value shall result in a larger or equal length of the listen queue. Implementations shall support values of backlog up to SOMAXCONN, defined in <sys/socket.h>.

    The implementation may include incomplete connections in its listen queue. The limits on the number of incomplete connections and completed connections queued may be different.

    The implementation may have an upper limit on the length of the listen queue-either global or per accepting socket. If backlog exceeds this limit, the length of the listen queue is set to the limit.

    If listen() is called with a backlog argument value that is less than 0, the function behaves as if it had been called with a backlog argument value of 0.

    A backlog argument of 0 may allow the socket to accept connections, in which case the length of the listen queue may be set to an implementation-defined minimum value.

    The socket in use may require the process to have appropriate privileges to use the listen() function.

RETURN VALUE

    Upon successful completions, listen() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.




listen()

mark a socket as accepting connections

    
 
SYNOPSIS

#include <sys/socket.h>

int listen(int s, int backlog);


DESCRIPTION

The listen() function marks a connection-mode socket (for example, those of type SOCK_STREAM), specified by the socket argument s, as accepting connections, and limits the number of outstanding connections in the socket's listen queue to the value specified by the backlog argument. The socket s is put into 'passive' mode where incoming connection requests are acknowledged and queued pending acceptance by the process.

The backlog parameter of this function is typically used by servers that could have more than one connection request at a time: if a connection request arrives with the queue full, the client receives an error with an indication of ECONNREFUSED.

listen() attempts to continue to function rationally when there are no available descriptors. It accepts connections until the queue is emptied. If descriptors become available, a later call to listen() or accept() re-fills the queue to the current or most recent backlog', if possible, and resume listening for incoming connections.

An application may call listen() more than once on the same socket. This has the effect of updating the current backlog for the listening socket. Should there be more pending connections than the new backlog value, the excess pending connections are reset and dropped.

backlog is limited (silently) to a reasonable value as determined by the underlying service provider. Illegal values are replaced by the nearest legal value.

PARAMETERS

s

    Is a descriptor identifying a bound, unconnected socket.

backlog

    Is the maximum length that the queue of pending connections may grow to. If this value is SOMAXCONN, then the underlying service provider responsible for socket s sets the backlog to a maximum "reasonable" value.

RETURN VALUES

If successful, listen() returns a value of zero. On failure, it returns -1 and sets errno to one of the following values:

EBADF

    s is not a valid descriptor
EINCONN

    s is already connected.
EINVAL

    s has not been bound to a local address.
EMFILE

    No more socket descriptors are available.
ENETDOWN

    Network subsystem has failed.
ENOBUFS

    No buffer space is available.
ENOTSOCK

    s is not a socket
EOPNOTSUPP

    s is not a socket that supports listen().



The listen Function:

The listen function is called only by a TCP server and it performs two actions:

    The listen function converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests directed to this socket.

    The second argument to this function specifies the maximum number of connections the kernel should queue for this socket.

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

int listen(int sockfd,int backlog);


This call returns 0 on success otherwise it gives you -1 on error.
Parameters:

    sockfd: is a socket descriptor returned by the socket function.

    backlog is the number of allowed connections.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值