已建立的连接被主机中的软件(Python套接字)中止
我正在尝试为我正在制作的游戏创建一个在线代码。显然,运行此代码会产生错误。错误为[WinError 10053]主机中的软件中止了已建立的连接 这是我的密码: 服务器 客户已建立的连接被主机中的软件(Python套接字)中止,python,sockets,server,Python,Sockets,Server,我正在尝试为我正在制作的游戏创建一个在线代码。显然,运行此代码会产生错误。错误为[WinError 10053]主机中的软件中止了已建立的连接 这是我的密码: 服务器 客户 import time class Network: def __init__(self): randomvar = "." while True: try: self.client = socket.soc
import time
class Network:
def __init__(self):
randomvar = "."
while True:
try:
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = "localhost" # For this to work on your machine this must be equal to the ipv4 address of the machine running the server
# You can find this address by typing ipconfig in CMD and copying the ipv4 address. Again this must be the servers
# ipv4 address. This feild will be the same for all your clients.
self.port = 5555
self.addr = (self.host, self.port)
self.id = self.connect()
break
except ConnectionRefusedError:
if randomvar != "Waiting for server...":
print("Waiting for server...")
randomvar = "Waiting for server..."
def getNumber(self):
pass
def connect(self):
self.client.connect(self.addr)
return self.client.recv(2048).decode()
def send(self, data):
"""
:param data: str
:return: str
"""
try:
self.client.send(str.encode(data))
reply = self.client.recv(2048).decode()
return reply
except socket.error as e:
return str(e)
n = Network()
print(n.send("Host"))
print(n.send("hello"))
在服务器上,它只接收Host
,而不接收hello
。这就是我得到错误的地方,但它不会告诉我它是哪一行
有什么帮助吗?您忽略了异常。相反,将其打印出来以了解问题所在:
回溯(最近一次呼叫最后一次):
线程客户端中的第39行文件“D:\temp\python\server.py”
id=int(arr[0])
ValueError:基数为10的int()的文本无效:“主机”
这就引出了这一行:
id = int(arr[0])
服务器似乎希望消息的格式为id:msg
,但客户端没有发送该格式的消息。它只是发送没有id的消息。您可以在服务器上检查这一点
arr = reply.split(":")
if len(arr) != 2 or !arr[0].isdigit():
# Handle error....
arr = reply.split(":")
if len(arr) != 2 or !arr[0].isdigit():
# Handle error....