首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Tic Tac Toe with sockets - sockets的问题

Python Tic Tac Toe with sockets - sockets的问题
EN

Stack Overflow用户
提问于 2019-10-07 15:51:14
回答 1查看 741关注 0票数 1

我正在尝试写一个使用python的套接字的Tic Tac Toe游戏。有一个服务器文件和一个客户端文件。当我运行它(目前都在我的计算机上)时,我先运行服务器文件,然后再运行客户机文件两次(对于X和O)。在某种程度上,我得到了一个套接字错误(在后面的注释中指定,在服务器文件的代码中指定)。

这是服务器文件中的main方法:

~~python

代码语言:javascript
复制
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发送或接收数据的请求被拒绝,因为套接字没有连接,并且(当使用sendto调用在数据报套接字上发送时)没有提供地址“

然后程序停止..。

代码语言:javascript
复制
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

这是客户端文件中的main方法:

~~python

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-07 16:04:14

来自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)会给你一个错误。您想要的是与发送时相同的东西,即使用两个连接。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58265424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档