Reputation: 51
I'm setting up a simple server and client. I tried the client code without connecting to the server, pygame loads and everything works. I do one extra step where the starting position of the first sprite is recieved by the server, game loads and the starting position is what it should be. Now the next step would be to continously send the players position whilst recieving the other players. Now I get the error:
File "test.py", line 89, in <module>
p2Pos = read_pos(send_package(make_pos((sprite1.x, sprite1.y))))
File "test.py", line 50, in read_pos
return int(str[0]), int(str[1])
ValueError: invalid literal for int() with base 10: ''
This tells me that the client and server are not communicating properly. Sockets are byte-streams and not message-streams. So I am obviously not sending and/or recieving further packages after the starting position. But I do not know how to counter this problem? I need to somehow tell the server how much data it can expect but I don't know how? Should the first byte it sends be the length of the package and when it recieves that should it enter a loop and read from the stream until it has read the length of the package it expects? How would I do that? None of the prior topics on this have been of enough help for me...
Server.py:
import threading
import sys
server = "127.0.0.1"
port = 2300
pos = [(0, 0), (200, 200)]
playerr = 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server, port))
except socket.error as e:
str(e)
s.listen(2)
print("Waiting for a connection, Server Started")
sys.stdout.flush()
def read_pos(str):
str = str.split(",")
return int(str[0]), int(str[1])
def make_pos(tup):
return str(tup[0]) + "," + str(tup[1])
def threaded_client(conn, player):
conn.sendall(str.encode(make_pos(pos[player])))
reply = ""
while True:
try:
data = read_pos(conn.recv(2048).decode())
pos[player] = data
if not data:
print("Disconnected")
break
else:
if player == 0:
reply = pos[1]
else:
reply = pos[0]
print("Received: ", data)
print("Sending : ", reply)
conn.sendall(str.encode(make_pos(reply)))
except:
break
print("Lost connection")
conn.close()
while True:
conn, addr = s.accept()
print("Connected to:", addr)
t = threading.Thread(target=threaded_client, args=(conn, playerr))
t.start()
playerr += 1
test.py(client):
import pygame as pg
import socket
def connect():
try:
client.connect(addr)
return client.recv(2048).decode()
except:
pass
def send_package(data):
try:
client.sendall(str.encode(data))
return client.recv(2048).decode()
except socket.error as e:
print(e)
def get_pos():
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
sprite1.vx = -0.3
if keys[pg.K_RIGHT]:
sprite1.vx = 0.3
if keys[pg.K_UP]:
sprite1.vy = -0.3
if keys[pg.K_DOWN]:
sprite1.vy = 0.3
def update_pos():
sprite1.x += sprite1.vx
sprite1.y += sprite1.vy
sprite1.vx, sprite1.vy = 0, 0
sprite1.rect = pg.Rect(sprite1.x, sprite1.y, 75, 75)
def update_other():
sprite2.rect = pg.Rect(sprite2.x, sprite2.y, 75, 75)
def stop_unboud():
if sprite1.x < 0:
sprite1.x = 0
if sprite1.y < 0:
sprite1.y = 0
if sprite1.x > 724:
sprite1.x = 724
if sprite1.y > 524:
sprite1.y = 524
def read_pos(str):
str = str.split(",")
return int(str[0]), int(str[1])
def make_pos(tup):
return str(tup[0]) + "," + str(tup[1])
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = "127.0.0.1"
port = 2300
addr = (server, port)
pos = read_pos(connect())
pg.init()
window = pg.display.set_mode((800, 600))
sprite1 = pg.sprite.Sprite()
sprite1.image = pg.Surface((75, 75))
sprite1.image.fill((255, 0, 0))
sprite1.x = pos[0]
sprite1.y = pos[1]
sprite1.vx, sprite1.vy = 0,0
sprite1.rect = pg.Rect(sprite1.x, sprite1.y, 75, 75)
sprite2 = pg.sprite.Sprite()
sprite2.image = pg.Surface((75, 75))
sprite2.image.fill((255, 0, 0))
sprite2.x = 200
sprite2.y = 200
sprite2.vx, sprite2.vy = 0,0
sprite2.rect = pg.Rect(sprite2.x, sprite2.y, 75, 75)
all_group = pg.sprite.Group([sprite1, sprite2])
run = True
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
p2Pos = read_pos(send_package(make_pos((sprite1.x, sprite1.y))))
sprite2.x = p2Pos[0]
sprite2.y = p2Pos[1]
update_other()
get_pos()
stop_unboud()
update_pos()
window.fill(0)
all_group.draw(window)
pg.display.flip()
pg.quit()
exit()````
Upvotes: 1
Views: 322
Reputation: 51
Found the problem. In moving the sprite I change the sprite1.x and sprite1.y by decimal places, hence not sending over ints but most likely double/float values. Thus I changed the speed at which the sprite moves to 1 from 0.3 and now I am not getting this error message.
Upvotes: 1