python中socket在bind之后listen,比如listen(5)这个5,是对什么的限制
高洛峰
高洛峰 2017-04-18 09:40:49
0
3
872

服务端

# coding:utf-8   
# server.py                                                               
import socket

sock_server = socket.socket()
sock_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock_server.bind(("127.0.0.1",1234))
sock_server.listen(3)
print("server start ...")
while True:
    tmp = sock_server.accept()
    print(tmp,"\n\n") 

客户端

# coding:utf-8                                                                  
import socket


def make_socks(sock_num):
    socks = []
    for i in range(sock_num):
        tmp_sock = socket.socket()
        tmp_sock.connect(("127.0.0.1",1234))
        socks.append(tmp_sock)
    

    return socks

if __name__ == "__main__":
    make_socks(5)

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

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
大家讲道理

The size of the tcp connection queue, that is, the number of connections

TCP handshake and socket communication details

伊谢尔伦

Python has not been studied, but in C, it refers to the queue length of the client that can establish a connection on the server side, indicating that the server has experienced two handshakes waiting for the accept system call, which is mentioned in the book Linux High-Performance Server Programming , now this backlog is just a suggested value for the kernel, in fact it can be slightly larger. If I remember correctly, this is how it was described

刘奇
sock_server.listen(5)  # 开始监听TCP传入连接

The value passed in specifies the maximum number of connections the operating system can suspend before rejecting the connection. This value should be at least 1, and 5 will be fine for most applications.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template