I'm pretty new to python and I'm trying to build a multiplayer tic tac toe game where 2 players can play at once. I have used sockets, select and threads to accomplish this.
However, I am at a dead end at a particular problem.
SERVER
def incoming_message(self, client_socket: socket.socket, client_address):
while True:
try:
client_msg = client_socket.recv(2048)
move = client_msg.decode('utf-8')
print(f"Player {self.players[f'{client_address}'].symbol} chose: {move}")
self.send_board()
self.send_updates(client_address)
except Exception:
import traceback
print(traceback.format_exc())
socket.close()
The above function is in its own thread. If it receives a 'message' or rather, a move from the player, it will try to send the players an updated board as well as what position the player had chosen.
def send_updates(self, client_address):
self.connected_clients[f"{client_address}"].send(
bytes(f"You {self.players[f'{client_address}'].symbol} sent ", encoding='utf-8'))
def send_board(self):
for all_connected_clients in self.connected_clients:
self.connected_clients[all_connected_clients].send(pickle.dumps(self.board))
send_board function sends the player the updated board using pickle as the board itself is a 2d array which I would format it on the client side.
send_updates is just a regular str that is being sent over as well.
CLIENT
def incoming_message(self, client_socket):
while True:
ready_sockets, _, _ = select.select(
[client_socket], [], [], 60
)
if ready_sockets:
try:
for sockets in ready_sockets:
received_board = sockets.recv(1024)
self.board = pickle.loads(received_board)
self.print_board()
except Exception as e:
print(sockets.recv(1024).decode())
print(e)
on the client side, function incoming_message is in its own thread as well. I used select to check if the sockets that I had received from the server is readable.
If it is readable, i would try to unpack the bytes that are sent over.
I tried to use try and except error handling to look for any unpickling error. From what i had understood, if pickle.loads() is unable to load the received bytes, the except block would handle it by printing the sockets bytes as it as.
Any help or advices would be appreciated! Thanks
EDIT:
This is the error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte