服务端
# 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个连接应该报错的
大家讲道理2017-04-18 09:42:49
The size of the tcp connection queue, that is, the number of connections
TCP handshake and socket communication details
伊谢尔伦2017-04-18 09:42:49
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
怪我咯2017-04-18 09:42:49
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.