简体   繁体   English

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

[英]An established connection was aborted by the software in your host machine - Python socket

I'm trying to create an online code to a game I'm making.我正在尝试为我正在制作的游戏创建在线代码。 Obviously, running this code gives an error.显然,运行此代码会出错。 The error is [WinError 10053] An established connection was aborted by the software in your host machine.错误是[WinError 10053] 已建立的连接已被主机中的软件中止。

Here's my code: SERVER这是我的代码:服务器

from _thread import *
import sys

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

host = socket.gethostname()
server = 'localhost'
port = 5555

server_ip = socket.gethostbyname(server)

try:
    s.bind((server, port))
except socket.error as e:
    print(str(e))

s.listen(2)
print("Currently waiting for other users...")

currentId = "0"
pos = ["0:50,50", "1:100,100"]
def threaded_client(conn):
    global currentId, pos
    conn.send(str.encode(currentId))
    currentId = "1"
    reply = ''
    while True:
        try:
            data = conn.recv(2048)
            reply = data.decode('utf-8')
            if not data:
                conn.send(str.encode("Goodbye"))
                break
            else:
                print("Recieved: " + reply)
                arr = reply.split(":")
                id = int(arr[0])
                pos[id] = reply

                if id == 0: nid = 1
                if id == 1: nid = 0

                reply = pos[nid][:]
                print("Sending: " + reply)

            conn.sendall(str.encode(reply))
        except:
            break

    print("Connection Closed")
    conn.close()

while True:
    conn, addr = s.accept()

    start_new_thread(threaded_client, (conn,))

CLIENT客户

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"))

On the server, the only things it receives Host , but not hello .在服务器上,它接收的唯一内容是Host ,而不是hello That's where I get the error, but It won't tell me which line it is.那就是我得到错误的地方,但它不会告诉我它是哪一行。

Any help?有什么帮助吗?

You are ignoring the exception.你忽略了例外。 Instead, print it out to get an idea of what is wrong:相反,将其打印出来以了解问题所在:

Traceback (most recent call last):
  File "D:\temp\python\server.py", line 39, in threaded_client
    id = int(arr[0])
ValueError: invalid literal for int() with base 10: 'Host'

This leads to this line:这导致了这一行:

id = int(arr[0])

It looks like the server is expecting the messages to be in the form of id:msg but the client is not sending that.看起来服务器期望消息采用id:msg的形式,但客户端没有发送该消息。 It is just sending the message without an id.它只是发送没有 id 的消息。 You can check this in the server.您可以在服务器中检查这一点。

arr = reply.split(":")
if len(arr) != 2 or !arr[0].isdigit():
    # Handle error....

暂无
暂无

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

相关问题 Django:您主机中的软件中止了已建立的连接 - Django : An established connection was aborted by the software in your host machine ConnectionAbortedError: [WinError 10053] 已建立的连接被主机中的软件中止 - ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine Django:[WinError 10053] 已建立的连接被主机中的软件中止 - Django: [WinError 10053] An established connection was aborted by the software in your host machine Python 到 MySQL 错误:[WinError 10053] 已建立的连接被主机中的软件中止 - Python to MySQL error: [WinError 10053] An established connection was aborted by the software in your host machine ConnectionAbortedError: [WinError 10053] 已建立的连接被主机中的软件中止了 GeckoDriver 和 Firefox - ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine with GeckoDriver and Firefox Django ConnectionAbortedError: [WinError 10053] 已建立的连接被主机中的软件中止 - Django ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine Django React js:ConnectionAbortedError:[WinError 10053] 已建立的连接被主机中的软件中止 - Django React js: ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine 收到此错误“ConnectionAbortedError:[WinError 10053] 已建立的连接被主机中的软件中止” - getting this error " ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine" 如何解决此问题:ConnectionAbortedError: [WinError 10053] 已建立的连接被主机中的软件中止 - how can fixed this problem: ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine Django: ConnectionAbortedError: [WinError 10053] 已建立的连接被主机中的软件中止 - Django: ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM