服务器:
import socket
import random
randnumber = random.randrange(1,20)
ip = '127.0.0.1'
port = 5000
n = 1
tries = 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((ip,port))
s.listen(5)
print("...<<")
while True :
(c,a) = s.accept()
print("Sundedemenos me ton ",a)
data = c.recv(1024).decode()
if (data):
wellcome = ("Welcome to the guess the number game!")
c.send(wellcome.encode())
gues =("What is your guess? ")
c.send(gues.encode())
value = int(c.recv(1024).decode())
print (value)
goal = abs(value-randnumber)
print(goal)
c.close()
客户端:
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("127.0.0.1",5000))
hello = ("hello")
s.send(hello.encode())
wellcome = s.recv(1024).decode()
print (wellcome)
gues = s.recv(1024).decode()
print (gues)
while (gues):
if not gues:
break
else:
value = input()
s.send(value.encode())
s.close()
您好, 为什么客户端运行1000亿次为什么休息不起作用? 我只是想让游戏在服务器和客户端上运行,例如#34;欢迎来到游戏&#34; &GT; &#34;你的猜测是什么?&#34; &GT; &#34; 5&#34; &GT; &#34;你很亲密&#34; &GT; &#34; 6&#34; &GT; &#34;你是赢家&#34;&gt;结束
答案 0 :(得分:0)
这是一个永远正确的循环,因为gues
永远不会改变!
while (gues):
if not gues:
break
else:
value = input()
s.send(value.encode())
另外,我认为两个while
循环都在错误的位置:
希望它有所帮助,遗憾的是没有时间实施它,但我认为这是一个很好的练习:)