Search code examples
python-2.7pygameserversocket

how to send pygame screen using socket programming


I tried to send my pygame screen to my another pc using socket programming but it give error in client.py Actually in new in pygame so why these happen i didn't get it

Traceback (most recent call last): File "webclient.py", line 24, in image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string ValueError: String length does not equal format and resolution size

and on server.py

Traceback (most recent call last): File "/home/gaurav/Desktop/Games_Project/panda.py", line 35, in image = screen.blit(bg, (0, 0)) AttributeError: 'NoneType' object has no attribute 'blit'

Here is my 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()

Here is my 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()

Solution

  • First Error:

    Traceback (most recent call last): File "webclient.py", line 24,
    in image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string
    ValueError: String length does not equal format and resolution size
    

    I'm not sure you understand exactly what blit does. It will push an image to a spot on the screen. Its return value is a pygame.Rect object, but it looks like you were expecting an image.

    Then, you are attempting to send this pygame.Rect object, and this error will occur, because your code is expecting a string that holds an image, but it is receiving a string representing a pygame.Rect object.

    It appears you are trying to send the bg image. To do that, change this code inside of server.py:

    data = pygame.image.tostring(image, "RGB")  # convert captured image  to string, use RGB color scheme
    

    to

    data = pygame.image.tostring(bg, "RGB")  # convert captured image  to string, use RGB color scheme
    

    Second Error:

    Traceback (most recent call last): File "panda.py", line 35,
    in image = screen.blit(bg, (0, 0)) AttributeError: 'NoneType' object has no attribute 'blit'
    

    Look at these lines:

    screen=pygame.display.set_mode((width,height))
    screen=pygame.display.set_caption("Game by Players")
    

    In the first line, you correctly create a pygame.Surface to display on. In the second line, you are attempting to set the window caption. This is where your error occurs. Look at the pygame docs for this function:

    pygame.display.set_caption()
        Set the current window caption
        set_caption(title, icontitle=None) -> None
    

    The function returns None! This means your screen is now equal to None when you attempt to call screen.blit, hence the error.

    To fix the problem change this line:

    screen=pygame.display.set_caption("Game by Players")
    

    to

    pygame.display.set_caption("Game by Players")