IMPLEMENTATION OF THE CLIENT - AlexEmerton/py_udp_cw GitHub Wiki

A client was implemented using the standard socket datagram communication.

 #prompt an ID from the user
  clientID = input('Please enter your ID (1-100): ')
 #validation
 while int(clientID) < 1 or int(clientID) > 100:
  print('\nClientID value is unacceptable please try again')
  clientID = input('Please enter your ID (1-100): ')

First, the client ID is stored and checked against the validation rules.

 # prompt and int from the user
 clientINT = input('Please enter an integer of your choice: ')

Then the client inputted integer is also stored.

 # sending 2 packets
 sock.sendto(clientID.encode('utf-8'), server_address)
 sock.sendto(clientINT.encode('utf-8'), server_address)

After this 2 packets are encoded (utf-8) and sent to the server.

 # receiving packets
 while state == 0:
  print("\nwaiting to receive...")
  received = sock.recv(4096)
  received = received.decode("utf-8", "ignore")
  if received == "You win!" or received == "You lose!":
   # closing the client
   state += 1
   print("\nTHE RESULT IS: ", received)
   sock.close()
   elif received == "Game over!":
    state += 1
    print("\nGAME IS OVER!")
    sock.close()

Finally, the above bit of code only runs while the state of the client is equal to 0. Messages are being constantly received or waited to be received. In case the messages are triggering any of the 3 messages specified, the program displays them to the client. After this, the state of the program is set to 1 (offline) and the socket is closed.

⚠️ **GitHub.com Fallback** ⚠️