0
votes

I have one tcp server and two connected clients. This is a tic tac toe game, so clients make their moves one-by-one. The server sends a packet to the client, to say that it allows to input, while the second client just wait. The problem is when the second client waiting it can write something, but it will not send to the server until its turns. server output. second client. So the problem is when turn turns to the second client and he wrote something before it, it's sent to the server. How can i "clear" everything before "Your turn, enter (row col)"

"Your turn, enter (row col)" - means server sent packet, and wait for user input

"Everything ok" - means user input delivered to the server

Server:

            if is_x:
                players[0].send(b'\0x01')
                client_move = sock_manipulation.receive_move(players[0])
                if client_move:
                    players[0].send('Everything ok'.encode())
                    print(f'Client move: {client_move}')
                else:
                    print('Nothing received!')
                is_x = False
            else:
                players[1].send(b'\0x01')
                client_move = sock_manipulation.receive_move(players[1])
                if client_move:
                    players[1].send('Everything ok'.encode())
                    print(f'Client move: {client_move}')
                else:
                    print('Nothing received!')
                is_x = True

Client:

    while 1:
    command = sock.recv(2048)
    if command == b'\0x01':
        move = pickle.dumps(input('Your turn, enter (row col): ').split())
        sock.send(move)
        accept = sock.recv(2048)
        print(accept.decode())
1
do you want to clear your output on the screen? - gavin
no, not the output. The second client wrote something before "Your turn..." and I want to ignore this, and ask for input. - mlsdmitry

1 Answers

0
votes

Two possibilities:

  • Close client connection when it is not his turn
    • the client should try to reconnect but server will only accept if it is its turn
  • Server should always receive from a client, but if it is not its turn the recv data is ignored

Personally I would use the second option.