我正在用python制作游戏。它使用套接字。这是我第一次使用套接字。
这是我的代码的一部分:
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# get local machine name
host = socket.gethostname()
port = 9999
# bind to the port
serversocket.bind((host, port))
# queue up to 5 requests
serversocket.listen(5)
player = 1
keys = []
playerSockets = []
print "Waiting for players..."
while True:
# establish a connection
clientsocket, addr = serversocket.accept()
key = id_generator()
keys.append(key)
playerSockets.append(clientsocket)
clientsocket.send(str(player)+"\r\n")
clientsocket.send(str(key)+"\r\n")
print "Player " + str(player) + " connected."
player = player + 1
if player > 3 or currentEpochtime() > endConn:
break
print str(player-1) + " active players.\n"
if(player < 3):
print "Not enough Players. Closing..."
serversocket.close()
quit()
#Other Stuff Continues
现在,它只在我的代码中出现下一个发送语句后才发送上述数据(玩家和密钥)。但是,下一个发送语句永远不会发送,因为它正在等待来自客户端的回复。但是客户端没有发送任何东西,因为它还没有从服务器中的前一个发送语句中接收到数据。
很长一段时间以来,我一直在努力寻找解决方案,但无法做到。
请帮忙!