简体   繁体   English

Python套接字服务器/客户端未循环

[英]Python socket server/client not looping

I'm working on a number guessing game handled over two clients connected via socket, but for some reason after the initial loop it stops working and doesn't send/receive anything. 我正在开发一个通过两个通过套接字连接的客户端处理的猜数字游戏,但是由于某些原因,初始循环后它停止工作并且不发送/接收任何内容。 The code all executes fine the first time around, but no matter what I change, the second time around I either get an error at the line guessNumber = int(s.recv(4096).decode() in the server file - where it reports an error trying to change '' into an int. This implies it's not receiving a value (in these instances, the Client program doesn't receive the "What is your guess? " string either). The code below however just doesn't do anything after that initial loop, If you guess the number on that first loop then it works fine, your score is written to a file and they both end as they should. 第一次代码都可以很好地执行,但是无论我做了什么更改,第二次代码我都在服务器文件中的guessNumber = int(s.recv(4096).decode()行中遇到错误-报告尝试将''更改为int的错误。这意味着它没有接收到值(在这些情况下,Client程序也没有接收到"What is your guess? "字符串)。但是下面的代码只是没有不能在该初始循环后执行任何操作,如果您猜到了第一个循环中的数字,那么它工作正常,您的乐谱将被写入文件,并且两者均应按预期结束。

But if you don't then it doesn't do anything either, so any help would be appreciated. 但是,如果您不这样做,它也不起作用,因此,我们将不胜感激。 It's running in Python 3.4.2 using the IDLE IDE. 它使用IDLE IDE在Python 3.4.2中运行。

Server: 服务器:

import socket
import random

l = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

score = 0
guessNumber = 0

def within(value, goal, n):
    numDifference = abs(value - goal)
    if (numDifference <= n):
        return True
    else:
        return False

scoreFile = open('scores', 'a')
randomNumber = random.randrange(0, 11)

l.bind(("127.0.0.1", 4001)) 
l.listen(5) 
print("Waiting...")

while True:
    (s,ca)=l.accept()   
    print("Connection from", ca)

    while (not(within(guessNumber, randomNumber, 0))):
        s.sendall("What is your guess? ".encode())
        guessNumber = int(s.recv(4096).decode())
        score += 1

        print ("%d" % guessNumber)

        if (within(guessNumber, randomNumber, 0)):
            s.sendall(("You guessed correctly! \n Your score is %d" % score).encode())
            scoreFile.write("Player %s has a score of %d \n" % (ca, score))
            scoreFile.close()
            s.sendall("1".encode())
        elif (within(guessNumber, randomNumber, 3)):
            s.sendall("You are getting close!".encode())
            s.sendall("0".encode())
        else:
            s.sendall("You are way off".encode())
            s.sendall("0".encode())

    s.close()  

Client: 客户:

import socket

gameOver = False
guessNumber = 0

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 4001))

while (gameOver != "1"):
    #Send guess
    guessNumber = input(s.recv(4096).decode()) 
    s.sendall(guessNumber.encode())

    #Recieve result
    print(s.recv(4096).decode())

    #Check whether game is over or not
    gameOver = s.recv(4096).decode()

print ("Game Over")
s.close()   

It turns out the client was closing it's socket after the first iteration because it ended with a receive. 事实证明,客户端在第一次迭代后就关闭了套接字,因为它以接收结束。 I've added some send confirmations after each receive to tell the Server it was performed successfully, and now all works as intended! 我在每次接收后都添加了一些发送确认,以告知服务器已成功执行,现在所有操作都按预期进行了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM