我正在尝试用python编写带有井字孔的井字游戏。有一个服务器文件和一个客户端文件。当我运行它(当前全部在我的计算机上)时,我运行服务器文件,然后运行两次客户端文件(对于X和O)。有时我会收到套接字错误(稍后在注释中在服务器文件的代码中指定)。
这是服务器文件中的主要方法:
~~ python
mat = initialize() # initializes the board (a matrix)
server_socket = socket.socket()
server_socket.bind(("0.0.0.0", 5555))
server_socket.listen(2)
(ix_socket, client_address) = server_socket.accept()
(o_socket, client_address) = server_socket.accept()
ix_socket.send("X".encode('utf-8')) # sends "X" to the first client
o_socket.send("O".encode('utf-8')) # sends "O" to the second client
mat_str = mat_to_mat_str(mat) # this function turns the board from matrix into a string so I can send it
ix_socket.send(mat_str.encode('utf-8'))
o_socket.send(mat_str.encode('utf-8'))
row=server_socket.recv(1)
col = server_socket.recv(1)
mat[row][col]="X" # should insert row and col received from client X
mat_str=mat_to_mat_str(mat) # should transform the modified board from matrix into string so I can send it
ix_socket.send(mat_str.encode('utf-8')) # should send mat_str to X client
o_socket.send(mat_str.encode('utf-8')) # should send mat_str to O client
这是客户端文件中的主要方法:
~~ python
client_socket = socket.socket()
client_socket.connect(("127.0.0.1", 5555))
SIGN = client_socket.recv(1) # receives b"X" or b"O"
SIGN = SIGN.decode('utf-8')
mat_str = client_socket.recv(1024) # receives initial board in byte form
mat_str = mat_str.decode('utf-8') # turns bytes into string
mat = mat_str_to_mat(mat_str) # turns string into matrix (with function)
print("Initial board:")
print_mat(mat) # prints initial board (with no X's nor O's on it)
print("\r")
if SIGN == "X": # if you happend to be the first one to connect you are "X", and you should select a slot - for example 1a - where you want to place an X.
print("Insert row (1, 2, 3...):")
row = input()
print("Insert col (a, b, c...):")
col = input()
col = ord(col) - 96
col=str(col)
client_socket.send(row.encode('utf-8')) # sending row index
# oops, it is not being received by the server.
# from now on the program doesn't work if you are client X.
client_socket.send(col.encode('utf-8')) # sending col index
mat_str=client_socket.recv(1024) # receiving modified board in byte form
mat_str=mat_str.decode('utf-8') # turning it into string form
mat_str=mat_str_to_mat(mat_str) # turning it into matrix form
print("your board now:")
print_mat(mat) # printing modified board
elif SIGN == "O": # If you happened to be the second one to connect, you are O.
print("Waiting for X to play.")
mat_str = client_socket.recv(1024)
# receiving new board in byte form. It doesn't receive anything until client X plays. And he can't play, because the server doesn't receive the row index.
#so the following while loop runs forever:
while len(mat_str)==0: # waiting until it receives something
mat_str = client_socket.recv(1024)
mat_str = mat_str.decode('utf-8') # turning modified board from bytes into string
mat = mat_str_to_mat(mat_str) # turning string into matrix with function
print("your board now:")
print_mat(mat) # printing modified board
从https://docs.python.org/3/library/socket.html#timeouts-and-the-accept-method处的文档开始:
Also note that the server does not sendall()/recv() on the socket it is listening on but on the new socket returned by accept().
所以server_socket.recv(1)
将给您一个错误。您想要的是与发送时使用的相同的东西,即使用两个连接。
您能分享您的密码吗?您是否在您的GitHub帐户中找到了它们?请分享给我,我将非常感谢!