1

My target is print the message from function result on the client's screen. But only ONE client can received the message...


The part of client.py is here

def PlayGame(clientSocket, msg):
invalid = "NO!"
if ("/guess " in msg):
 msg1 = msg.split(" ")[1]
 print("Hi1\n")
 if msg1 == "true" or msg1 == "false":
 print("Hi11")
 clientSocket.send(msg1.encode())
 print(clientSocket.recv(1024).decode())
 print("!")
 return '1'
 else:
 clientSocket.send(invalid.encode())
 print(clientSocket.recv(1024).decode())
 print("2")
 return '2'
elif msg == "":
 return '2'
else:
 clientSocket.send(invalid.encode())
 print(clientSocket.recv(1024).decode())
 print("3")
 return '2'
def main(argv):
 msg=""
 while (PlayGame(clientSocket, msg)!=1):
 msg = input()

Any part of the server.py

guess_box = []
guess = bool(random.randint(0, 1))
 def result(connectionSocket, guess_box, addr, addr_l):
 a = 0
 if(guess_box[0] == guess_box[1]):
 msg = "Tie!!"
 connectionSocket.send(msg.encode())
 return '2'
 elif(guess_box[0] == guess):
 msg = "Player 1 Wins!"
 a+=1
 connectionSocket.send(msg.encode())
 return '2'
 elif(guess_box[1] == guess):
 msg = "Player 2 Wins!"
 a+=1
 connectionSocket.send(msg.encode())
 return '2'
 
 def TF(connectionSocket, var, guess_box, addr, addr_l):
 msg = connectionSocket.recv(1024).decode()
 print("recv:",msg)
 if(msg == 'true'):
 msg = 'True'
 var = str(var)
 msg = bool(msg == var)
 guess_box.append(msg)
 return 'ok'
 elif(msg == 'false'):
 msg = 'False'
 var = str(var)
 msg = bool(msg == var)
 guess_box.append(msg)
 return 'ok'
 else:
 print(msg)
 statement = "4002 Unrecognized message!!"
 connectionSocket.send(statement.encode())
 return 'again'
class ServerThread(threading.Thread):
 def __init__(self, client):
 threading.Thread.__init__(self)
 self.client = client
 def run(self):
 ...
 print("guess is:", guess)
 
 while (len(guess_box) != 2):
 TF(connectionSocket, guess, guess_box, addr, addr_l)
 
 print("start")
 result(connectionSocket, guess_box, addr, addr_l)
 ...
asked Oct 27, 2021 at 12:53
4
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Oct 27, 2021 at 13:42
  • Some of the issues: GameHallMsg does return 1 for 'list' and invalid 'enter' messages while it should rather return 'wait'. The while(1): #waiting the other player is an end- and pointless loop. Commented Oct 28, 2021 at 15:31
  • 1
    @Armali I have updated the code and write the expected output. Thanks for the advises again! Commented Oct 28, 2021 at 15:37
  • I think the problem is: the second client that input the command /guess, cannot receive the message. Commented Oct 28, 2021 at 16:35

1 Answer 1

1

Regarding only the one problem you address:

print the message from function result on the client's screen. But only ONE client can received the message

The problem comes from the use of a different thread for each client. The thread which receives a guess as first stays in its

 while (len(guess_box) != 2):
 print(guess_box)
 TF(connectionSocket, guess, guess_box)

loop and waits for another message, which doesn't come. The thread which receives a guess as second sends the result to its own client only.

I don't think there's a sensible way to fix this while keeping this (削除) d (削除ここまで)threaded approach.

Can I change the structure of my code by using those functions I implemented?

Here's a substitute for the while True loop in server_run that doesn't require changes in those functions other than server_run.

 from select import select
 connections = []
 room_connection = {}
 for reads in iter(lambda: select([serverSocket]+connections, [], [])[0], []):
 for ready in reads: # for each socket which select reports is readable
 if ready == serverSocket: # it's the server socket, accept new client
 connectionSocket, addr = serverSocket.accept()
 connections.append(connectionSocket)# store the connection socket
 while RecvFromClient(connectionSocket) == "NO": pass
 else: # message (or disconnect) from a client
 try: var = GameHallMsg(ready, ready, connections)
 except socket.error: var = 'bye'
 if var == 'bye': # client finished, remove from list
 connections.remove(ready)
 ready.close()
 elif var == 'wait': # store first player's connection
 room_connection[current_rm_num.pop()] = ready
 elif var == 'NO':
 rtn_msg_4 = "4002 Unrecognized message"
 ready.send(rtn_msg_4.encode())
 elif var == 'jump':
 readyroom = current_rm_num.pop()
 # find and notify other player in the room
 other = room_connection.pop(readyroom)
 rtn_msg_2 = "3012 Game started. Please guess true or false"
 other.send(rtn_msg_2.encode())
 print("guess is:", guess)
 # query and inform both players
 guess_box = []
 while TF(ready, True, guess_box) != 'ok': pass
 while TF(other, True, guess_box) != 'ok': pass
 result(ready, guess_box, ('', 0), [0])
 result(other, guess_box, ('', 1), [0, 1])
 room[readyroom] = 0
answered Oct 28, 2021 at 17:39
Sign up to request clarification or add additional context in comments.

9 Comments

Can I change the structure of my code by using those functions I implemented? I am so sorry that I am not familiar with threading
This can be possible if errors in those functions are corrected, namely the indexing of state, which in RecvFromClient is by username.index, but in GameHallMsg is by addr_l.index.
(And at one place state is set to the room number, while otherwise it's used as a counter.)
thanks for answering my doubts! Acutally I've found a "solution" for my problem on github:github.com/yeeaayee/Computer_Network_Coursework/blob/master/… But can I use my existing code to do the samething with the code above by just correcting the problems you rise? Thank you again!!!
Really thanks for your help!! But why is the change in server_run but not in the run in ServerThread? Isn't the run in ServerThread control the whole thread? I am so sorry that I am just learning multithread in other course recently..
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.