0
有沒有辦法停止while循環,直到客戶端連接到此服務器? 服務器shuld爲每個新客戶端連接創建一個新線程,這有可能嗎?Python套接字等待客戶端連接
import socket
import threading
def clientdialog(myS):
conn, addr = myS.accept()
print ("Connection from: " + str(addr))
while 1:
data = conn.recv(1024).decode("utf-8")
if not data or data == 'q':
break
print ("from connected user: " + str(data))
host = "192.168.1.3"
port = 1998
mySocket = socket.socket()
mySocket.bind((host,port))
while True:
mySocket.listen(10)
#whait until socket don't connect
try:
threading._start_new_thread(clientdialog, (mySocket))
except:
print("error starting thread")
線程部分不工作,但阻擋呼叫完美,謝謝 – Racerider
@Racerider很高興我能幫上忙。我在這裏寫的代碼是一個有代表性的例子。有幾個補充它應該工作。祝一切順利。 –