我想通过套接字发送PyGame图像与Python 3.5,我总是有一个错误。Python3.5通过套接字发送对象(Pygame凸轮图像)
我得到的图像,我把它,然后我发送它。
在客户端,我收到它,我unpickle它,我展示它。
,但我得到了一个错误:
fenetre.blit(img, (20, 30)) pygame.error: display Surface quit
这里是我的服务器代码(在一个至极发送图像):
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0", (680, 480))
cam.start()
class Streaming(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
s = socket.socket()
s.bind(('192.168.1.158', 12801))
s.listen(1)
while True:
sc, info = s.accept()
print("Video client connected : "+str(info))
try:
while True:
image = cam.get_image()
str_img = pickle.dumps(image)
sc.send(str_img)
print(str_img)
print("Sending Image")
time.sleep(0.005)
except Exception as e:
print(str(e))
以及客户端的代码:
fenetre = pygame.display.set_mode((900, 900))
class Receiving(Thread):
def __init__(self):
Thread.__init__(self)
self.clock = pygame.time.Clock()
def run(self):
global fenetre
si = socket.socket()
si.connect(("192.168.1.158", 12801))
while True:
img = si.recv(4096)
img = pickle.loads(img)
fenetre.blit(img, (20, 30))
pygame.display.flip()
self.clock.tick(60)
预先感谢!
您发送'680x480' ='326400'字节(或者甚至'326400 * 3'如果像素使用3个字节)但你只收到'4096'。在使用'pickle'和'blit'之前,您必须收到所有数据。 – furas
谢谢。但现在,问题是我无法在单个“recv”否接收大量数据?我该怎么做326500字节? – Minege
你必须使用循环多次接收'4096'字节。如果你知道有多少字节将被发送,那么你知道你需要多少次接收'4096'字节。 – furas