2017-10-19 1 views
0

私はハングマンゲームを作っています。私はサーバーに== 7を失うなら、loseGame = trueを送信しようとしています。クライアントサイドでは、loseGameが真であれば、ゲームが失われたことを表示します。私はsendとrecvにマッチしましたが、動作していないし、手紙を推測するための入力を求め続けます。私が間違っていることを知っていますか?ソケットプログラムPython

私は問題がどこにあるのか、問題がどこにあるのかを説明します。

ありがとうございました!

サーバー:

import sys 

# Import socket library 
from socket import * 

if sys.argv.__len__() != 2: 
    serverPort = 5895 
# Get port number from command line 
else: 
    serverPort = int(sys.argv[1]) 

# Choose SOCK_STREAM, which is TCP 
# This is a welcome socket 
serverSocket = socket(AF_INET, SOCK_STREAM) 

serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) 

# Start listening on specified port 
serverSocket.bind(('', serverPort)) 

# Listener begins listening 
serverSocket.listen(1) 

print("The server is ready to receive") 

#Set secret word 
word = 'arkansas' 
linesForString = ''  
#Prints out number of letters 
for x in word: 
    linesForString += '_' 

newWord = 'arkansas' 

# Wait for connection and create a new socket 
# It blocks here waiting for connection 
connectionSocket, addr = serverSocket.accept() 
win = ' ' 
#Sends lines of words 
linesInBytes = linesForString.encode('utf-8') 
connectionSocket.send(linesInBytes) 

lose = 0 
while 1: 


    l = list(word) 
    list2 = list(linesForString) 

    win = False 

    while 1: 

     while win == False: 
      losee = 0 
      # Receives Letter 
      letter = connectionSocket.recv(1024) 
      letterString = letter.decode('utf-8') 

      for x in range(len(list2)): 
       if(list2[x] == '_'): 
        if(letterString == l[x]): 
         list2[x] = letterString  

      for x in range(len(word)): 
       if(letterString == word[x]): 
        losee = -1 
      if (losee != -1): 
       lose += 1 

      print(lose) 
      newWord = "".join(list2) 

      #Sends newWord 
      newWordInBytes = newWord.encode('utf-8') 
      connectionSocket.send(newWordInBytes, lose) 


      if(newWord == 'arkansas'): 
       win = True 
       winGame = 'You have won the game' 
       winGameInBytes = winGame.encode('utf-8') 
       connectionSocket.send(winGameInBytes) 
       connectionSocket.close() 


      if(lose == 7): 
       loseGame = 'true' 
       connectionSocket.close() 
      else: 
       loseGame = 'false' 

      #THIS IS WHERE THE PROBLEM IS 
      loseGameInBytes = loseGame.encode('utf-8') 
      connectionSocket.send(loseGameInBytes) 

# Close connection to client but do not close welcome socket 
connectionSocket.close() 

クライアント:

import sys 

# Import socket library 
from socket import * 

if sys.argv.__len__() != 3: 
    serverName = 'localhost' 
    serverPort = 5895 
# Get from command line 
else: 
    serverName = sys.argv[1] 
    serverPort = int(sys.argv[2]) 

# Choose SOCK_STREAM, which is TCP 
clientSocket = socket(AF_INET, SOCK_STREAM) 

# Connect to server using hostname/IP and port 
clientSocket.connect((serverName, serverPort)) 

#Recieves lines of words 
linesInBytes = clientSocket.recv(1024) 
lines = linesInBytes.decode('utf-8') 
for x in lines: 
    print(x, end = " ") 

while 1: 

    # Get letter from user 
    print('\n') 
    letter = input('Guess a letter: ') 

    # Sends letter 
    letterBytes = letter.encode('utf-8') 
    clientSocket.send(letterBytes) 

    #Recieves newWord 
    newWordInBytes = clientSocket.recv(1024) 
    newWord = newWordInBytes.decode('utf-8') 

    for x in newWord: 
     print(x, end = " ") 
    print(" ") 

    if(newWord == 'arkansas'): 
     winGameInBytes = clientSocket.recv(1024) 
     winGame = winGameInBytes.decode('utf-8') 
     print(winGame) 
     clientSocket.close() 
     break 

    #THIS IS WHERE THE PROBLEM IS 
    loseGame = " " 
    loseGameInBytes = clientSocket.recv(1024) 
    loseGame = loseGame.encode('utf-8') 

    if(loseGame == "true"): 
     print('You have lost the game!') 
     clientSocket.close() 

答えて

0

私はそれが働いて得ることができるまでに問題なかった物事のカップル: 1.通知を使用すると、入力された使用raw_inputを取るときには、それ以外の場合に私のために墜落した。 2.サーバーに問題があると思ったのは本当に1つでした。その後、あなた偶然近くに接続してゲームは、あなたはあなたが私が私が固定した後、これはコードです

を発行しましたので、私はprint(str(x) + " "),にprint文を変更する代わりにloseGame = loseGameInBytes.encode('utf-8') 4のloseGame = loseGame.encode('utf-8')を持っているクライアントに 3を失われた後送信それ。通知私は変更し、必要ではないサーバー内の休憩を追加し、彼らは1つのゲームの終わりにサーバーを閉じる原因になります。

サーバコード:

import sys 

# Import socket library 
from socket import * 

if sys.argv.__len__() != 2: 
    serverPort = 5895 
# Get port number from command line 
else: 
    serverPort = int(sys.argv[1]) 

# Choose SOCK_STREAM, which is TCP 
# This is a welcome socket 
serverSocket = socket(AF_INET, SOCK_STREAM) 

serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) 

# Start listening on specified port 
serverSocket.bind(('', serverPort)) 

# Listener begins listening 
serverSocket.listen(1) 

print("The server is ready to receive") 

#Set secret word 
word = 'respecttheplatypus' 
linesForString = ''  
#Prints out number of letters 
for x in word: 
    linesForString += '_' 

newWord = 'respecttheplatypus' 

# Wait for connection and create a new socket 
# It blocks here waiting for connection 
connectionSocket, addr = serverSocket.accept() 
win = ' ' 
#Sends lines of words 
linesInBytes = linesForString.encode('utf-8') 
connectionSocket.send(linesInBytes) 

lose = 0 
while 1: 


    l = list(word) 
    list2 = list(linesForString) 

    win = False 

    while 1: 

     while win == False: 
      losee = 0 
      # Receives Letter 
      letter = connectionSocket.recv(1024) 
      letterString = letter.decode('utf-8') 

      for x in range(len(list2)): 
       if(list2[x] == '_'): 
        if(letterString == l[x]): 
         list2[x] = letterString  

      for x in range(len(word)): 
       if(letterString == word[x]): 
        losee = -1 
      if (losee != -1): 
       lose += 1 

      print(lose) 
      newWord = "".join(list2) 

      #Sends newWord 
      newWordInBytes = newWord.encode('utf-8') 
      connectionSocket.send(newWordInBytes, lose) 


      if(newWord == 'respecttheplatypus'): 
       win = True 
       winGame = 'You have won the game' 
       winGameInBytes = winGame.encode('utf-8') 
       connectionSocket.send(winGameInBytes) 
       connectionSocket.close() 
      else: 

       if(lose == 7): 
        loseGame = 'true' 
        loseGameInBytes = loseGame.encode('utf-8') 
        print(loseGame) 
        connectionSocket.send(loseGameInBytes) 
        connectionSocket.close() 
        break 
       else: 
        loseGame = 'false' 
       #THIS IS WHERE THE PROBLEM IS 
        loseGameInBytes = loseGame.encode('utf-8') 
        print(loseGame) 
        connectionSocket.send(loseGameInBytes) 
     break 
    break 

クライアントコード:

import sys 

# Import socket library 
from socket import * 

if sys.argv.__len__() != 3: 
    serverName = 'localhost' 
    serverPort = 5895 
# Get from command line 
else: 
    serverName = sys.argv[1] 
    serverPort = int(sys.argv[2]) 

# Choose SOCK_STREAM, which is TCP 
clientSocket = socket(AF_INET, SOCK_STREAM) 

# Connect to server using hostname/IP and port 
clientSocket.connect((serverName, serverPort)) 

#Recieves lines of words 
linesInBytes = clientSocket.recv(1024) 
lines = linesInBytes.decode('utf-8') 
for x in lines: 
    print(str(x) + " "), 

while 1: 

    # Get letter from user 
    print('\n') 
    letter = raw_input('Guess a letter: ') 

    # Sends letter 
    letterBytes = letter.encode('utf-8') 
    clientSocket.send(letterBytes) 

    #Recieves newWord 
    newWordInBytes = clientSocket.recv(len(linesInBytes)) 
    newWord = newWordInBytes.decode('utf-8') 

    for x in newWord: 
     print(str(x) + " "), 
    print(" ") 

    if(newWord == 'respecttheplatypus'): 
     winGameInBytes = clientSocket.recv(1024) 
     winGame = winGameInBytes.decode('utf-8') 
     print(winGame) 
     clientSocket.close() 
     break 

    #THIS IS WHERE THE PROBLEM IS 
    loseGame = " " 
    loseGameInBytes = clientSocket.recv(1024) 
    loseGame = loseGameInBytes.encode('utf-8') 

    print("lose game is" + str(loseGame)) 
    if(loseGame == "true"): 
     print('You have lost the game!') 
     clientSocket.close() 
関連する問題