当前位置: 技术问答>linux和unix
请问关于listen(int sockfd, int n)函数的问题
来源: 互联网 发布时间:2015-11-12
本文导语: 近日学习socket编程,对于listen函数有点不大明白,我看资料上写的是:是在请求连接的队列中允许的连接数目。对于这个队列的说法我有点不明白就是,是不是如果我执行了accept后,队列中的请求连接数就会减少一个...
近日学习socket编程,对于listen函数有点不大明白,我看资料上写的是:是在请求连接的队列中允许的连接数目。对于这个队列的说法我有点不明白就是,是不是如果我执行了accept后,队列中的请求连接数就会减少一个?然后客户端就可以继续增加一个连接请求到队列中,只要服务器端不断的accept,那么客户端就可以不断的增加连接数。
那这个参数对于控制客户端的连接数似乎没多大作用啊。比如我现在想控制客户端的连接数,我是不是只能通过其他方式控制呢?比如说控制我的服务器程序的子进程数或者线程数
那这个参数对于控制客户端的连接数似乎没多大作用啊。比如我现在想控制客户端的连接数,我是不是只能通过其他方式控制呢?比如说控制我的服务器程序的子进程数或者线程数
|
下面的几句话也许能解释你的问题:
listen的第二个参数在不同系统中的实现是不同的,就像fierygnu(va_list) 说的那样。但是内核维护的这两个序列(一个完成连接队列,一个未完成连接队列),在三次握手后才修改队列(tcp的话)。
The listen function is called only by a TCP server and it performs two actions:
1、When a socket is created by the socket function, it is assumed to be an active socket, that is, a client socket that will issue a connect. The listen function converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests directed to this socket. In terms of the TCP state transition diagram (Figure 2.4), the call to listen moves the socket from the CLOSED state to the LISTEN state.
2、The second argument to this function specifies the maximum number of connections the kernel should queue for this socket.
To understand the backlog argument, we must realize that for a given listening socket, the kernel maintains two queues:
An incomplete connection queue, which contains an entry for each SYN that has arrived from a client for which the server is awaiting completion of the TCP three-way handshake. These sockets are in the SYN_RCVD state (Figure 2.4).
A completed connection queue, which contains an entry for each client with whom the TCP three-way handshake has completed. These sockets are in the ESTABLISHED state (Figure 2.4).
The backlog argument to the listen function has historically specified the maximum value for the sum of both queues. (listen的第二个参数通常设定为这两个队列之和)
There has never been a formal definition of what the backlog means. The 4.2BSD man page says that it "defines the maximum length the queue of pending connections may grow to." Many man pages and even the POSIX specification copy this definition verbatim, but this definition does not say whether a pending connection is one in the SYN_RCVD state, one in the ESTABLISHED state that has not yet been accepted, or either. The historical definition in this bullet is the Berkeley implementation, dating back to 4.2BSD, and copied by many others.
listen的第二个参数在不同系统中的实现是不同的,就像fierygnu(va_list) 说的那样。但是内核维护的这两个序列(一个完成连接队列,一个未完成连接队列),在三次握手后才修改队列(tcp的话)。
The listen function is called only by a TCP server and it performs two actions:
1、When a socket is created by the socket function, it is assumed to be an active socket, that is, a client socket that will issue a connect. The listen function converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests directed to this socket. In terms of the TCP state transition diagram (Figure 2.4), the call to listen moves the socket from the CLOSED state to the LISTEN state.
2、The second argument to this function specifies the maximum number of connections the kernel should queue for this socket.
To understand the backlog argument, we must realize that for a given listening socket, the kernel maintains two queues:
An incomplete connection queue, which contains an entry for each SYN that has arrived from a client for which the server is awaiting completion of the TCP three-way handshake. These sockets are in the SYN_RCVD state (Figure 2.4).
A completed connection queue, which contains an entry for each client with whom the TCP three-way handshake has completed. These sockets are in the ESTABLISHED state (Figure 2.4).
The backlog argument to the listen function has historically specified the maximum value for the sum of both queues. (listen的第二个参数通常设定为这两个队列之和)
There has never been a formal definition of what the backlog means. The 4.2BSD man page says that it "defines the maximum length the queue of pending connections may grow to." Many man pages and even the POSIX specification copy this definition verbatim, but this definition does not say whether a pending connection is one in the SYN_RCVD state, one in the ESTABLISHED state that has not yet been accepted, or either. The historical definition in this bullet is the Berkeley implementation, dating back to 4.2BSD, and copied by many others.
|
控制客户端的数量不是这个来控制的,而是控制子进程的数量,我是用个全局变量childnum,
fork返回值大于0的那个分支中childnum+1
signal(SIGCHLD,sig_chld); //处理死掉的子进程,获取SIGCHLD信号
void sig_chld(int signo)//儿子一中断就消息触发到此函数,然后在这里childnum-1;
{
childnum-1;
}
这样你在fork之前判断一下childnum是否大于你的最多连接数,这样就可以了
fork返回值大于0的那个分支中childnum+1
signal(SIGCHLD,sig_chld); //处理死掉的子进程,获取SIGCHLD信号
void sig_chld(int signo)//儿子一中断就消息触发到此函数,然后在这里childnum-1;
{
childnum-1;
}
这样你在fork之前判断一下childnum是否大于你的最多连接数,这样就可以了