Maxpnl
Maxpnl

Reputation: 98

Socket and pygame

I'm trying to write a simple game but every time I start it, Pygame crashes.

Here's the client code:

import socket, pygame, sys
port = 5000
host_server = "localhost"
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host_server, port))
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('Animation')
WHITE = (255, 255, 255)
catImg = pygame.image.load('cat.png')
dogImg = pygame.image.load('dog.png')
catx = 0
caty = 0
dogx = 0
dogy = 0
while True:
    tasto = pygame.key.get_pressed()
    DISPLAYSURF.fill(WHITE)
    dogx = client_socket.recv(1024)
    client_socket.send(str(catx))
    if tasto[K_RIGHT]:
        catx += 5
    elif tasto[K_DOWN]:
        caty += 5
    elif tasto[K_LEFT]:
        catx -= 5
    elif tasto[K_UP]:
    caty -= 5
    DISPLAYSURF.blit(catImg, (catx, caty))
    DISPLAYSURF.blit(dogImg, (dogx, dogy))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()
    fpsClock.tick(FPS)

And here's the server code:

import pygame, sys, socket
from pygame.locals import *
host = "localhost"
port = 5000
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen(5)
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('Animation')
WHITE = (255, 255, 255)
catImg = pygame.image.load('cat.png')
dogImg = pygame.image.load('dog.png')
catx = 0
caty = 0
dogx = 0
dogy = 0
while True:
    client_socket, address = server_socket.accept()
    tasto = pygame.key.get_pressed()
    DISPLAYSURF.fill(WHITE)
    dogx = client_socket.recv(1024)
    client_socket.send(catx)
    if tasto[K_RIGHT]:
        catx += 5
    elif tasto[K_DOWN]:
        caty += 5
    elif tasto[K_LEFT]:
        catx -= 5
    elif tasto[K_UP]:
        caty -= 5
    DISPLAYSURF.blit(catImg, (catx, caty))
    DISPLAYSURF.blit(dogImg, (dogx, dogy))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()
    fpsClock.tick(FPS)

I know the code seems a bit messed up but I'm new in python and I'd like if you can explain to me my error. The problem might be in the recv size, but as I said, I'm new to Python so I'd like if you can explain to me how can I calculate the bytes of the message (if that's the problem).

Upvotes: 0

Views: 2205

Answers (2)

user3689258
user3689258

Reputation: 41

I'm going to refer you to http://stackoverflow.com/questions/17667903/python-socket-receive-large-amount-of-data. Read the accepted answer marked with the green checkmark. The recv(1024) function is most likely your problem. It is important to remember that the sockets you have created do what's called blocking. This means that if you call dogx = client_socket.recv(1024), but no data has been transmitted to the socket, then the function will wait FOREVER until some data has been transmitted to the socket. The other problem is the buffer size. It doesn't look to be the problem with your code, but if you tried to send the socket a large amount of data, you would have to call client_socket.recv(1024) multiple times to get the whole string of data. Refer to the link I posted.

Hope this helps! Good Luck!

Upvotes: 1

user58697
user58697

Reputation: 7923

What kind of crash do you have? Is there a stack trace, or it just hangs?

I can understand hanging, because both your server and your client begin communication with recv call, which never returns (nothing ever has been sent).

Upvotes: 2

Related Questions