Python 聊聊socket中的listen()参数(数字)到底代表什么

疑问

在调用socket的时候,我们会使用到listen()函数,里面有个参数叫backlog, 例如:socket.listen(5). 那么这个数字5到底代表什么意思呢?网上有很多种说法,讲的都是概念,很多都是复制粘贴,容易让人误解。

解答

下面使用具体的代码片段来讲解:

这是一个电脑本机模拟客服端及服务端的程序,主要功能是建立socket连接后,客户端输入关键字查询对应的段子,服务器端返回结果。服务器可以满足同时服务两个客户的查询操作此程序中还包含了信号量和多线程

以listen(1),参数设置为1进行讲解,是不是将参数设置为1后,开启3个客服连接就会报错呢?并不是!

如下图所示:

那么问题来了!

问题1:

如上例代码中,有listen(1),这个1是指什么?只能与1个socket建立链接吗? 为什么我用上面的代码可以创建大于1个的tcp连接却没报错,如果用网上其他人的说法理论上大于1个连接应该报错的!

因为:listen(n)传入的值, n表示的是服务器拒绝(超过限制数量的)连接之前,操作系统可以挂起的最大连接数量。n也可以看作是"排队的数量"

问题2:

既然没有报错,为什么没有打印用户3的地址?

因为:服务器正在处理用户1和用户2,没有空闲去接待用户3,所以用户3去排队了。

问题3:

为什么服务器能同时处理用户1和用户2?

因为:

这里用到了多线程和信号量,信号量设置为2,也就是允许并发数为2,服务器开启了两个线程,能同时分别处理用户1和用户2。

总结

socket.listen(n)

简单来说,这里的nt表示socket的”排队个数

一般情况下,一个进程只有一个主线程(也就是单线程),那么socket允许的最大连接数为: n + 1如果服务器是多线程,比如上面的代码例子是开了2个线程,那么socket允许的最大连接数就是: n + 2换句话说:排队的人数(就是那个n) + 正在就餐的人数(服务器正在处理的socket连接数) = 允许接待的总人数(socket允许的最大连接数)

补充:关于Socket.listen方法的一点体悟

前言

最近在接触Socket的的时候,关于其中的listen方法感到不解,于是对其进行了一番研究,得出了一点体悟,特此记录。

详解

让我们先来看看listen方法在python3.6文档说明:

socket.listen([backlog])

Enable a server to accept connections. If backlog is specified, it must be at least 0 (if it is lower, it is set to 0); it specifies the number of unaccepted connections that the system will allow before refusing new connections. If not specified, a default reasonable value is chosen.

启用服务器以接受连接。如果指定backlog,则必须至少为0(如果低于0,则设置为0);它指定系统在拒绝新连接之前将允许的未接受连接的数量。如果未指定,则选择默认的合理值。

Changed in version 3.5: The backlog parameter is now optional.

在版本3.5中已更改: backlog参数现在是可选的。

起初我看了这说明想当然的以为是可以接入的Client上限,不过实践过后发现并非如此。在网上找的解答基本上就是文档所言的复述,后来请教了专业人士后,方知这涉及到Socket的底层知识。

在了解listen方法之前,首先我们需要了解connect方法和accept方法,以下是文档说明:

socket.connect(address)

Connect to a remote socket at address. (The format of address depends on the address family ― see above.)

在地址连接到远程套接字。(地址的格式取决于地址系列 - 请参见上文)

If the connection is interrupted by a signal, the method waits until the connection completes, or raise a socket.timeout on timeout, if the signal handler doesn't raise an exception and the socket is blocking or has a timeout. For non-blocking sockets, the method raises an InterruptedError exception if the connection is interrupted by a signal (or the exception raised by the signal handler).

如果连接被信号中断,则该方法等待直到连接完成,或者如果信号处理程序没有引发异常并且套接字正在阻塞或者已经阻塞,则在超时时引入socket.timeout超时。对于非阻塞套接字,如果连接被信号中断(或由信号处理程序引发的异常),则该方法引发InterruptedError异常。

Changed in version 3.5: The method now waits until the connection completes instead of raising an InterruptedError exception if the connection is interrupted by a signal, the signal handler doesn't raise an exception and the socket is blocking or has a timeout (see the PEP 475 for the rationale).

在版本3.5中已更改:该方法现在等待直到连接完成,而不是提高InterruptedError异常,如果连接被信号中断,信号处理程序不引发异常,套接字阻塞或超时(参见 PEP 475)。

socket.accept()

Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.

接收一个连接.该socket 必须要绑定一个地址和监听连接.返回值是一对(conn, 地址)其中conn是新 t4 > socket对象可用于在连接上发送和接收数据,address是连接另一端的套接字的地址。

The newly created socket is non-inheritable.

新创建的套接字non-inheritable。

Changed in version 3.4: The socket is now non-inheritable.

在版本3.4中更改:套接字现在是不可继承的。

Changed in version 3.5: If the system call is interrupted and the signal handler does not raise an exception, the method now retries the system call instead of raising an InterruptedError exception (see PEP 475 for the rationale).

在版本3.5中更改:如果系统调用中断并且信号处理程序没有引发异常,则此方法现在重试系统调用,而不是引发InterruptedError异常 PEP 475)。

相比listen方法,它俩就好理解多了,一个是Client用于连接Server的方法,一个是Server用于接收Client的连接申请的方法。

但事实上accept方法一次只能接收一个Client的连接申请,而Client则是多个的,这样Socket会设计一个队列来存储Client的连接申请则是理所当然的。于是accept便从这个队列里提取首位成员处理即可。

以下是示意图:

如此便很清晰了,backlog参数的含义便是这个队列的最大值,也就是同时受理连接申请的最大值。关于backlog该设置为多少,从Skynet得到的参考为32。如果满了便需要Client重新connect。以上listen方法之谜便解开了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持 。如有错误或未考虑完全的地方,望不吝赐教。

相关文章

发表新评论