python中的listen()方法参数

2024-04-23 12:03:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,最近对socket编程很感兴趣。从youtube视频中,我正在构建一个简单的服务器,但是我没有很好地使用listen()方法。我知道“它监听”传入的连接,但我在文档中没有得到“最大排队连接数”的概念。你能用外行的话解释一下这个概念,这样我就能更好地理解了吗?在


Tags: 方法文档服务器概念视频youtube编程socket
1条回答
网友
1楼 · 发布于 2024-04-23 12:03:46

Python中的^{}调用底层^{} syscall

listen() marks the socket referred to by sockfd as a passive socket, that is, as a socket that will be used to accept incoming connection requests using accept(2).

被动套接字是您非正式地称之为服务器的套接字。在

The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later reattempt at connection succeeds.

换句话说,当您调用sock.listen(5)并且在调用accept之前有6个连接请求进来,其中一个请求被丢弃。实际上,the value is only a hint to the OS。在

除非您的应用程序或它的使用场景非常特别,否则传入任何值(通常引用5)并完成。只要确保在accept调用之间没有太多开销,挂起的连接队列将永远不会满,而且一开始很少使用。在

相关问题 更多 >