我有一台应该接受多个客户端的服务器,但出于某种原因,当它为客户端启动一个新线程时,它不会继续while循环,直到客户端断开连接。
此外,while循环停止select.select并且不会继续,直到有sock.accept的挂起连接。在将self.serverstatus值更改为0之后,它也没有正确断开并使套接字关闭
以下是服务器的代码
编辑:将包含ct.run()的行修复为ct.start()
def serverstart(self):
self.buttonswitch("1")
self.host = self.intip
self.port = 5000
s = socket.socket(socket.AF_INET6)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((self.host, self.port))
inputs = [ s ]
s.listen(5)
self.serverstatus = "1"
while True:
inputready, outputready, exceptready = select.select(inputs, [], [])
if inputready != " " and self.serverstatus == "1":
c, addr = s.accept()
ct = threading.Thread(target=self.client_thread, args=[c, addr])
ct.start()
elif self.serverstatus == "0":
break
s.close()
print "Closing socket"
self.buttonswitch("0")
def client_thread(self, c, addr):
print "Connection from: " + str(addr)
while True:
data = c.recv(1500)
print len(data)
if not data:
break
data = str(data).upper()
c.send(data)
if self.serverstatus == "0":
break
c.close()