【发布时间】:2017-04-05 05:20:10
【问题描述】:
我尝试使用套接字编程将我的 pygame 屏幕发送到我的另一台电脑,但它在 client.py 中给出错误实际上在 pygame 中是新的,所以为什么会发生这些我没有得到它
Traceback(最近一次调用最后一次):文件“webclient.py”,第 24 行,在 image = pygame.image.fromstring(dataset,(320,240),"RGB") # 从字符串中转换接收到的图像 ValueError: String length does not equal 格式和分辨率大小
在 server.py 上
Traceback(最近一次调用最后一次):文件 “/home/gaurav/Desktop/Games_Project/panda.py”,第 35 行,在 image = screen.blit(bg, (0, 0)) AttributeError: 'NoneType' object has no attribute 'blit'
这是我的 server.py
import pygame
import socket
import os
import time
width=800
height=600
fps=60
port=5012
imgdir=os.path.join(os.path.dirname(__file__),"img")
serversocket =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serversocket.bind(("",port))
serversocket.listen(1)
pygame.init()
clock=pygame.time.Clock()
screen=pygame.display.set_mode((width,height))
screen=pygame.display.set_caption("Game by Players")
bg = pygame.image.load(os.path.join(imgdir,"starfield.png")).convert()
running=True
while running:
clock.tick(fps)
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
running = False
connection, address = serversocket.accept()
image = screen.blit(bg, (0, 0))
pygame.display.flip()
data = pygame.image.tostring(image, "RGB") # convert captured image to string, use RGB color scheme
connection.sendall(data)
connection.close()
pygame.quit()
这是我的 client.py
import socket
import pygame
import sys
host = "127.0.0.1"
port=5012
screen = pygame.display.set_mode((320,240),0)
while True:
clientsocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect((host, port))
received = []
# loop .recv, it returns empty string when done, then transmitted data is completely received
while True:
recvd_data = clientsocket.recv(230400)
if not recvd_data:
break
else:
received.append(recvd_data)
dataset = ''.join(received)
image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string
screen.blit(image,(0,0)) # "show image" on the screen
pygame.display.update()
# check for quit events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
【问题讨论】:
标签: python-2.7 pygame serversocket