Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pygame在套接字接收数据时停止响应_Python_Sockets_Pygame - Fatal编程技术网

Python pygame在套接字接收数据时停止响应

Python pygame在套接字接收数据时停止响应,python,sockets,pygame,Python,Sockets,Pygame,我正在尝试用pygame和socket制作一个在线tictactoc游戏。一切都很顺利。但有一个小问题是,当服务器或客户端处于on接收模式并等待接收数据(如果需要4秒或更长时间)时。然后pygame的GUI停止响应。但如果数据被发送,它就会开始正常工作。它发生在接收数据时,花费了4秒或更长的时间。请让我解决这个问题 server.py代码 import socket import pygame import sys IP = socket.gethostbyname(socket.getho

我正在尝试用pygame和socket制作一个在线tictactoc游戏。一切都很顺利。但有一个小问题是,当服务器或客户端处于on接收模式并等待接收数据(如果需要4秒或更长时间)时。然后pygame的GUI停止响应。但如果数据被发送,它就会开始正常工作。它发生在接收数据时,花费了4秒或更长的时间。请让我解决这个问题

server.py代码

import socket
import pygame
import sys


IP = socket.gethostbyname(socket.gethostname())
PORT = 5555
Biended_adrs = (IP, PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(Biended_adrs)
server.listen()
conn, addr = server.accept()
print(f"{addr} connected!")

def Send(row, col):
    data = str((row, col))
    conn.send(data.encode())

def Receive():
    data = conn.recv(1024)
    return data.decode()




pygame.init()
display = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Server")

GRAY = (100, 111, 111)
LineWidth = 10

def DrawLine():
    pygame.draw.line(display, GRAY, (0, 200), (600, 200), LineWidth) # h1
    pygame.draw.line(display, GRAY, (0, 400), (600, 400), LineWidth) # h2
    pygame.draw.line(display, GRAY, (200, 0), (200, 600), LineWidth) # v1
    pygame.draw.line(display, GRAY, (400, 0), (400, 600), LineWidth) # v2

DrawLine()


firstPlayer = conn.recv(1024)
player = firstPlayer.decode()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            mouseX = event.pos[0]
            mouseY = event.pos[1]


            clickedRow = int(mouseY // 200)
            clickedCol = int(mouseX // 200)


            if player == "1":
                try:
                    Send(clickedRow, clickedCol)
                    player = "2"
                    conn.send(player.encode())
                except Exception:
                    pass

            else:
                pass
        if player == "2":
            try:
                Clientdata = Receive()
                print(Clientdata, "Client Send!")
                player = conn.recv(1024).decode()
                print(player)
                receive = False
            except Exception:
                pass

        else:
            pass


        pygame.display.update()
client.py代码

import socket
import pygame
import sys

pygame.init()
display = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Client")


HOST = socket.gethostbyname(socket.gethostname())
PORT = 5555

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST, PORT))

draw = DrawLine()

def Send(row, col):
    data = str((row, col))
    client.send(data.encode())

def Receive():
    data = client.recv(1024)
    return data.decode()

GRAY = (100, 111, 111)
LineWidth = 10

def DrawLine():
    pygame.draw.line(display, GRAY, (0, 200), (600, 200), LineWidth) # h1
    pygame.draw.line(display, GRAY, (0, 400), (600, 400), LineWidth) # h2
    pygame.draw.line(display, GRAY, (200, 0), (200, 600), LineWidth) # v1
    pygame.draw.line(display, GRAY, (400, 0), (400, 600), LineWidth) # v2


firstPlayer = "1"
client.send(firstPlayer.encode())
player = "1"

while True:
    for event in pygame.event.get():
        If event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            mouseX = event.pos[0]
            mouseY = event.pos[1]
            clickedRow = int(mouseY // 200)
            clickedCol = int(mouseX // 200)

            if player == "2":
                try:
                    Send(clickedRow, clickedCol)
                    player = "1"
                    client.send(player.encode())
                except Exception:
                    pass


        if player == "1":
            try:
                data = Receive()
                print(data, "Server Send!")
                player = client.recv(1024).decode()
                print(player)
            except Exception:
                pass
        else:
            pass


        pygame.display.update()

如果您查阅文档:

recv(bufsize[,flags])从套接字接收数据。回归 值是表示所接收数据的字节对象。最大值 一次接收的数据量由bufsize指定。见 Unix手册第recv(2)页,了解可选参数的含义 旗帜;它默认为零

它指向您可以在底层操作系统实现中使用的标志。我不知道如何实现这一点,但您想要的功能是传递
MSG\u DONTWAIT
。这样,如果消息尚未到达,您就可以了解它。这意味着您的程序可以继续设置动画,稍后再试。您需要同时返回
None
,并调整代码以检测到该错误,然后重试,直到消息到达

Python中还有:
socket.SOCK\u NONBLOCK
。这可能是要传递的标志:

conn.recv(1024, flags = socket.SOCK_NONBLOCK)

但在尝试解码之前,不要忘记检查返回值。

我仍然感到困惑。但是我在看了你的答案后尝试了这个标志,但是它说“AttributeError:module'socket'没有属性'SOCK\u NONBLOCK'”。你能给我举个例子看看下面的代码吗