2011-10-01 85 views
2

我用pygame,imageGrab和套接字创建了一个程序,但它不起作用。它应该使用ImageGrab将服务器的打印屏幕转换为字符串,并将其发送到客户端。但是,由于接收信息,并将其转换为图像客户端引发错误:Pygame不能用套接字工作

image = pygame.image.frombuffer(img, (800,600), "RGB") 
ValueError: Buffer length does not equal format and resolution size 

代码服务器

import sys, os, socket, pygame 
from PIL import ImageGrab 
from pygame.locals import * 
print "streaming sever 0.1" 
try: 
    socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

except socket.error: 
    print "Error creating socket" 
    sys.exit(1) 

host = "127.0.0.1" 
port = raw_input("Port:") 

socket.bind((host, int(port))) 
socket.listen(2) 

socket2,client = socket.accept() 
print "Client conectado: " + str(client[0]) + "\nPorta usada: " + str(client[1]) 


#Capture and send 

while True: 
    img=ImageGrab.grab().resize((800,600)) 
    img.tostring() 
    socket2.sendall(img) 

socket2.close() 

代码客户端

import sys, os, socket, pygame 
from PIL import ImageGrab 
from pygame.locals import * 
print "streaming client 0.1" 

pygame.init() 

try: 
    s_client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
    print "streaming protocol started" 
except socket.error: 
    print "Error creating socket" 
    sys.exit(1) 

host = "127.0.0.1" 
porta = raw_input("Port:") 
q_bytes = raw_input("Enter the number of MBs to transfer: ") 

t_bytes = 1024*1024*int(q_bytes) 

try: 
    s_client.connect((host,int(porta))) 
except socket.error: 
    print "Error connecting to: " + host 
    sys.exit(1) 
print "Conectado!" 



size = width, height = 800, 600 
screen = pygame.display.set_mode(size) 

num = 0 

while True: 
    img = s_client.recv(t_bytes) 
    image = pygame.image.frombuffer(img, (800,600), "RGB") 
    screen.blit(image,(0,0)) 
    pygame.display.flip() 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      os._exit(1) 
#recebimento 

s_client.close() 
+0

Hola!目前我们在这里说英语。 Por favor traducir http://translate.google.com/ :) – Coffee

+1

嗨adel我编辑了这篇文章,其实我是用英文写的,但是在Chrome中有一个错误,那个部分是用葡萄牙语出来的,谢谢你让我知道以我的错误感到非常惭愧,我的第一个帖子在这里,并已经提出错误),直到更多 谢谢布鲁诺Tripoloni – Siberdroni

+0

对任何人寻找ImageGrab;显然它只是Windows。 – Thomas

回答

2

与插座的交易是你要确切知道您收到的邮件的时间长短,如果您计划继续使用相同的套接字连接。您似乎对q_bytes = raw_input("Enter the number of MBs to transfer: ")有一些想法,但我们需要知道,确切地说是,而不是最接近的MB。有时候这些信息会在数据的前面发送,所以我们可以先阅读,然后知道何时停止阅读。

这个例外是,如果我们不再需要连接;我们只是想要这张照片。在这种情况下,可以根据需要请求尽可能多的数据,我们会在最后得到一个空字符串。

至于recv的max_bytes参数,那只是一个最大值 - 还有另外一个依赖于硬件的最大强加给我们,因为我的测试是1MB。

下面的代码只是一直询问数据,当它收到一个空字符串时停止,因为没有更多的数据,然后将所有这些收集的数据组合成完整的字符串。

有许多抽象层次可以(也应该)建立起来,以便我们远离这些复杂问题,但下面的代码仅仅是你的工作,并带有一些不相关的部分。

Client.py

import sys, os, socket, pygame 
from pygame.locals import * 

pygame.init() 

s_client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 

host = "127.0.0.1" 
porta = 12350 
t_bytes = 1024*1024*1 

s_client.connect((host,int(porta))) 
print "Conectado!" 

size = width, height = 300, 500 
screen = pygame.display.set_mode(size) 

message = [] 
while True: 
    s = s_client.recv(t_bytes) 
    if not s: 
     break 
    else: 
     message.append(s) 
full_s = "".join(message) 
print 'string received size', len(full_s) 
image = pygame.image.frombuffer(full_s, size, "RGB") 
#image = pygame.image.fromstring(s, size, "RGB") 
screen.blit(image,(0,0)) 
pygame.display.flip() 
for event in pygame.event.get(): 
    if event.type == QUIT: 
     pygame.quit() 
     os._exit(1) 
    raw_input() 

s_client.close() 

Server.py

import sys, os, socket, pygame 
from pygame.locals import * 

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
host = "127.0.0.1" 
port = 12350 
socket.bind((host, int(port))) 
socket.listen(2) 
socket2,client = socket.accept() 
print "Client conectado: " + str(client[0]) + "\nPorta usada: " + str(client 

img = Image.open('tiny.jpg').resize((300, 500)) 
s = img.tostring() 
print 'size of string', len(s) 
socket2.sendall(s) 

socket2.close() 

编辑:按照马克的修正,LEN()调用以前__sizeof__()方法调用。 __sizeof__以字节为单位返回python对象的大小,而不是字符串中的字节数/字符数。

+0

小改进:'len(s)'而不是'___ sizeof __()'。 –