I don’t understand why the server doesn’t send the first message or the client never receives the first message from the server. Which prevents you from starting the game.
I already checked if there were port conflicts but nothing reported and the ports listen fine too.
The server is supposed to send “You start first” and “Opponent starts first” to clients so that the first player enters their first move. Which never happens.
The clients and server listen but no one sends the message.
I also checked that the host IP address and port number are in the correct form in the sendto.
It’s been 3 days I’m stuck I really need help.
Thanks in advance
#SERVER_CODE
import socket
import random
HOTE = "127.0.0.1"
PORT = 5555
board = [[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']]
#clients stores ip addresses and port of clients
clients = [("127.0.0.1", 5554), ("127.0.0.1", 5556)]
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tour_courant = None
try:
server_socket.bind((HOTE, PORT))
print("bind OK")
except socket.error:
print("bind fail")
def check_gagnant(board):
winning_combinations = [
[(0, 0), (0, 1), (0, 2)],
[(1, 0), (1, 1), (1, 2)],
[(2, 0), (2, 1), (2, 2)],
[(0, 0), (1, 0), (2, 0)],
[(0, 1), (1, 1), (2, 1)],
[(0, 2), (1, 2), (2, 2)],
[(0, 0), (1, 1), (2, 2)],
[(0, 2), (1, 1), (2, 0)]
]
for combo in winning_combinations:
if (board[combo[0][0]][combo[0][1]] == board[combo[1][0]][combo[1][1]] == board[combo[2][0]][combo[2][1]] and
board[combo[0][0]][combo[0][1]] != ' '):
return board[combo[0][0]][combo[0][1]]
return None
def print_board(board):
for row in board:
print('|'.join(row))
print('-' * 5)
#toss
if len(clients) == 2 and tour_courant is None:
tour_courant = random.choice([0, 1]) #tour_courant is a variable that indicates who is playing, it represents the player id.
first_player_address = clients[tour_courant]
second_player_address = clients[1 - tour_courant]
print(f"Premier joueur: {first_player_address}")
print(f"Deuxième joueur: {second_player_address}")
server_socket.sendto("You start first".encode('utf-8'), first_player_address)
server_socket.sendto("Opponent starts first".encode('utf-8'), second_player_address)
while True:
print("Waiting for a move...")
move, client_address = server_socket.recvfrom(1024)
move = move.decode('utf-8')
print(f"movement received {client_address}: {move}")
row, col = map(int, move.split(','))
player_id = clients.index(client_address)
if board[row][col] == ' ' and tour_courant == player_id:
board[row][col] = 'X' if player_id == 0 else 'O'
winner = check_gagnant(board)
board_str = 'n'.join(['|'.join(row) for row in board])
print_board(board)
for c in clients:
server_socket.sendto(board_str.encode('utf-8'), c)
if winner:
for c in clients:
server_socket.sendto(f"WINNER:{winner}".encode('utf-8'), c)
break
elif all(cell != ' ' for row in board for cell in row):
for c in clients:
server_socket.sendto("DRAW".encode('utf-8'), c)
break
else:
tour_courant = 1 - player_id
for c in clients:
server_socket.sendto(f"NEXT_MOVE".encode('utf-8'), clients[tour_courant])
#CODE_CLIENT1
import socket
HOTE = "127.0.0.1"
PORT_CLIENT = 5554
PORT_SERVEUR = 5555
def clientUDP():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.bind((HOTE, PORT_CLIENT))
server_address = (HOTE, PORT_SERVEUR)
while True:
print("Waiting for the message...")
message, address = client_socket.recvfrom(1024)
message = message.decode('utf-8')
print(f"Received from server: {message}")
if message == "You start first":
print("You start first")
move = input("Enter your move (row,col): ")
client_socket.sendto(move.encode('utf-8'), server_address)
elif message == "Opponent starts first":
print("Opponent starts first")
elif message == "The box is filled":
move = input("Enter your move (row,col): ")
client_socket.sendto(move.encode('utf-8'), server_address)
elif message == "NEXT_MOVE":
print("NEXT_MOVE")
move = input("Enter your move (row,col): ")
client_socket.sendto(move.encode('utf-8'), server_address)
else:
print("Updated game board:")
print(message)
if "WINNER:" in message:
winner = message.split(":")[1]
print(f"Game over! The winner is {winner}")
break
elif message == "DRAW":
print("Game over! It's a draw.")
break
clientUDP()
#CODE_CLIENT2
import socket
HOTE = "127.0.0.1"
PORT_CLIENT = 5556
PORT_SERVEUR = 5555
def clientUDP():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.bind((HOTE, PORT_CLIENT))
server_address = (HOTE, PORT_SERVEUR)
while True:
print("Waiting for the message...")
message, adress = client_socket.recvfrom(1024)
message = message.decode('utf-8')
print(f"Received from server: {message}")
if message == "You start first":
print("You start first")
move = input("Enter your move (row,col): ")
client_socket.sendto(move.encode('utf-8'), server_address)
elif message == "Opponent starts first":
print("Opponent starts first")
elif message == "The box is filled":
move = input("Enter your move (row,col): ")
client_socket.sendto(move.encode('utf-8'), server_address)
elif message == "NEXT_MOVE":
print("NEXT_MOVE")
move = input("Enter your move (row,col): ")
client_socket.sendto(move.encode('utf-8'), server_address)
else:
print("Updated game board:")
print(message)
if "WINNER:" in message:
winner = message.split(":")[1]
print(f"Game over! The winner is {winner}")
break
elif message == "DRAW":
print("Game over! It's a draw.")
break
clientUDP()
broques is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2