-1
チュートリアルからクライアントサーバーのセットアップを作成しましたが、サーバーは以下のwhileループでスタックされており、理由がわかりません。 私はここでの質問を検索し、それがうまくいくかどうかを確認しています。がPythonサーバーで機能しない場合は、
私はそれが何か簡単だろうと確信しています。私はPythonに慣れていないので、どんな助けにも感謝します。 これは私の最初の質問であることをご了承ください。
私は問題を考え出し3.5.2
# create a client thread
def clientthread(conn):
# sending message to the client
message = 'Welcome to the Server. Type something and hit Enter\n'
conn.send(message.encode()) # send only takes a string
while 1:
# receiving from client
data = conn.recv(1024)
if not data.decode(): break
reply = 'OK ' + data.decode()
conn.sendall(reply.encode())
# came out of loop
conn.close()
# keep server up
while 1:
# accept incomming connections
conn, addr = soc.accept()
# display client info
print('Connected with' + addr[0] + ':' + str(addr[1]))
# start a new thread takes function name and a tupal
start_new_thread(clientthread, (conn,))
soc.close()
'break'sは最も内側のループから抜けるだけです。言い換えれば、 'break'は接続を閉じるだけです(' clientthread'にある 'while'ループ) - サーバを強制終了しません(つまり' soc.accept() 'の前にループを終了しません)。 –
返信いただきありがとうございます、私はそれが内側のループを壊すことは知っていますが、それは問題ですが、私は何をしても、内側のループから壊れない。私はちょうど私が戻ってブレークをアクティブにする必要がありますが、それはしないので、私は接続を閉じることはできませんを考えた。 – popeyesfx
この問題を解明するために、私はtelnetを使ってサーバをテストしていましたが、サーバがデータとして見るように\ r \ nを最後に追加しました。 – popeyesfx