Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
已建立的连接被主机中的软件(Python套接字)中止_Python_Sockets_Server - Fatal编程技术网

已建立的连接被主机中的软件(Python套接字)中止

已建立的连接被主机中的软件(Python套接字)中止,python,sockets,server,Python,Sockets,Server,我正在尝试为我正在制作的游戏创建一个在线代码。显然,运行此代码会产生错误。错误为[WinError 10053]主机中的软件中止了已建立的连接 这是我的密码: 服务器 客户 import time class Network: def __init__(self): randomvar = "." while True: try: self.client = socket.soc

我正在尝试为我正在制作的游戏创建一个在线代码。显然,运行此代码会产生错误。错误为[WinError 10053]主机中的软件中止了已建立的连接

这是我的密码: 服务器

客户

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....