IMPLEMENTATION OF THE SERVER - AlexEmerton/py_udp_cw GitHub Wiki
The server side of the solution was created using a socket server framework. The solution created can work with two clients only each time. The code provided can be easily modified to work with more than 2 clients but for the sake of simplicity, it was kept to minimum requirements.
### BELOW ARE THE MOST IMPORTANT BITS OF THE CODE.
# adding TWO clients to the game
if len(connected) < 1:
connected.append(self.client_address)
stored_client = connected[0]
elif connected[0] == stored_client:
connected.append(self.client_address)
This bit of code ensures that 2 clients are connected and their IP addresses are kept in the cache. First client to join will be trigger the first if statement and be stored as the stored_client. The elif will always be equal to TRUE and make sure that the second clients IP is also stored.
# receiving messages
cid = self.request[0]
cint = self.request[1].recv(1024)
print("\nInteger received: ")
print(cint.decode("utf-8", "ignore"))
Receiving messages is done by requesting the function to call to itself for the message from the client. The request [0] is receiving the ID while request [1] is used to receive the integer from the client. Message needs to be decoded because it was coded into utf-8 prior to sending. Reason for that is that the socket interface does not support standard string encryption.
# maths
cint = int(cint.decode("utf-8", "ignore"))
count = (cint+count) % 10
This is the maths function which was described in the specification. The received message is turned into the integer, processed and stored in the count variable.
# make sure we have TWO clients connected
if len(connected) == 2:
This check is required to make sure the server has 2 clients connected to processing the integers.
# sending messages to client
if nOfWins < 3:
if count == 0:
self.request[1].sendto(win.encode("utf-8"), connected[0])
print("\nWINNER IS:", stored_client)
nOfWins += 1
# clearing the cache
del connected[:]
stored_client = ('null', 0)
else:
self.request[1].sendto(lose.encode("utf-8"), connected[0])
print("\n", stored_client, "lost!")
# clearing the cache
del connected[:]
stored_client = ('null', 0)
else:
self.request[1].sendto(stop.encode("utf-8"), connected[0])
print("\nTHE GAME IS OVER!")
# clearing the cache
del connected[:]
stored_client = ('null', 0)
This is executed after the above if returns true. First, number of winning messages are checked. If they are below specified margin the code checks the value in the count variable. In case count is equal to zero the program sends out the winning message (encoded in utf-8 to fit the interface) to the stored client. The number of wins is incremented by 1. After this, the cache is cleared by deleting the values in the connected [] list and resetting the stored clients’ value. Else the alternative message is sent out and the cache is cleared again. In case the number of winning messages is more than 3 the “Game over!” message is sent to the client and cache are cleared again.
# serve forever
if __name__ == "__main__":
server = socketserver.UDPServer(server_address, MyUDPHandler,
bind_and_activate=True)
server.serve_forever()
This bit of code is set outside the class to make sure it works all the time as opposed to working only when the client in connected. The if statement is chosen so it will be equal to TRUE and keep the server running.