Python 带套接字的井字游戏 - 套接字的问题

Python Tic Tac Toe with sockets - problems with the sockets

我正在尝试在 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)

这里我得到这个错误:"socket.error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"

然后程序停止...

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) 会给你一个错误。你要的和你发送的时候是一样的,就是用两个连接。