Morpion 2 players UDP request locally, 2 clients 1 server

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()

New contributor

broques is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật