#SERVER
import socket
from _thread import *
from plyer import Player, FootBall
import pickle
class GameServer:
def __init__(self):
self.Server = '127.0.0.1'
self.Port = 52674
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.PlayerId = 0
st1 = Player(630, 375, (255, 0, 0), 'Arsenal', 'ST')
lw1 = Player(900, 300, (255, 0, 0), 'Arsenal', 'CB')
rw1 = Player(370, 300, (255, 0, 0), 'Arsenal', 'RW')
st2 = Player(630, 300, (0, 255, 0), 'Spurs', 'ST')
lw2 = Player(370, 425, (0, 255, 0), 'Spurs', 'CB')
rw2 = Player(900, 425, (0, 255, 0), 'Spurs', 'RW')
self.football = FootBall(630, 375) <-- Object I wish to send to both players
self.Player1_slot = st1
self.Player2_slot = st2
self.p = [self.Player1_slot, self.Player2_slot] # Players that are currently being used
def Bind(self):
# Connects the client to the server
try:
self.s.bind((self.Server, self.Port))
except socket.error:
print("Error")
# Listens to maximum 2 players only
self.s.listen(2)
print("Waiting for a connection...")
def ReturnSocket(self):
return self.s
def ThreadedClient(self, conn, curr_player):
conn.send(pickle.dumps(self.p[curr_player])) <--- Objects I am currently able to send to both players
#conn.send(pickle.dumps(self.football))
while True:
# Continuously runs when the client is connected
try:
pos_data = pickle.loads(conn.recv(2048))
self.p[curr_player] = pos_data
if not pos_data:
print("Player Left")
break
else:
if curr_player == 1:
reply = self.p[0]
else:
reply = self.p[1]
reply2 = self.football
print("[PLAYER] Received: ", pos_data)
print("[BALL] Received", reply2)
print("[PLAYER] Sending: ", reply)
conn.sendall(pickle.dumps(reply))
#conn.sendall(pickle.dumps(reply2))
except:
break
print("Connection Lost")
conn.close()
def main():
GS = GameServer()
GS.Bind()
while True:
s = GS.ReturnSocket()
# Continuously looking for connections
conn, addr = s.accept()
print(addr, "has joined the game")
# Function runs in the background
start_new_thread(GS.ThreadedClient, (conn, GS.PlayerId))
GS.PlayerId += 1
if __name__ == '__main__':
main()
#CLIENT
import socket
import pickle
class Network:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = '127.0.0.1'
self.port = 52674
self.addr = (self.host, self.port)
self.p = self.Connect()
print(self.p)
def ReturnP(self):
return self.p
def Connect(self):
try:
self.client.connect(self.addr)
return pickle.loads(self.client.recv(4096))
except:
pass
def Send(self, data):
try:
print(data)
self.client.send(pickle.dumps(data))
reply = pickle.loads(self.client.recv(4096))
return reply
except socket.error as e:
return str(e)
I would like to send the self.football class to a client alongside with the current classes I have already sent. I have tried doing sendall.pickle.dumps(self.football) however the football does not send. How can I make it so that when it is sent it is separate to the other classes. Thank you in advanced