无法保持套接字活动Python

时间:2015-10-31 05:01:39

标签: python sockets python-3.x python-3.5

我正在尝试创建一个客户端/服务器控制台游戏,但我无法保持套接字处于活动状态,在我调用close()之前它似乎已关闭但我无法弄清楚为什么

我已经阅读了帖子here,但我在while循环之外调用了connect(),因为在我尝试运行它之前,这个逻辑已经对我有意义了,但我仍然从服务器shell获取错误:

ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

到目前为止,我没有在客户端shell上收到错误,它只是在启动后退出运行,这也是发生服务器错误时。
在修复注释中指出的错误后,客户端现在还显示一个错误,指出在尝试启动while循环时connected未定义,但应该在进入循环之前运行Connect()应该设置connected = True因此循环应该运行但不运行。我怀疑这与服务器问题有关,但这可能是什么问题?

我将下面的代码放在一起:

客户端

import socket

def GetData():
    data = s.recv(1000).decode()
    if (data == "closing"):
        connected = False
    else:
        print(data)

def SendData():
    tdata = input("")
    s.send(data.encode())


def Connect():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("127.0.0.1", 2222))
    connected = True


global connected
Connect()

while (connected == True):
    GetData()
    if (connected == False):
        s.close()
        break
    SendData()

服务器

import socket
import random

def TheMainGame():
    print ("started TheMainGame")  # Remove after debug
    def within(val, goal):
        print ("Runing 'within' function")  # Remove after debug
        i = abs(val - goal)
        if (i <= 5):
            return True
        else:
            return False

    def Guess():
        print ("Running 'Guess' function")  # Remove after debug
        while True:
            print ("Entered while loop")  # Remove after debug
            data = "What is your guess?"
            c.send(data.encode())
            print ("Sent data")  # Remove after debug
            t = c.recv(1000).decode()
            print ("Got data")  # Remove after debug
            if (t == x):
                data = "Correct!"
                c.send(data.encode())
                data = "closing"
                c.send(data.encode())
                c.close()
                break
            elif (within(t,x) == True):
                data = "You're close!"
                c.send(data.encode())
            elif (within(t,x) == False):
                data = "Try again"
                c.send(data.encode())
            else:
                data = "There was an error computing the value"
                c.send(data.encode())
                c.close()
                break

    x = random.randrange(1, 20, 1)
    Guess()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 2222))
s.listen(5)
global data
while True:
    (c,a) = s.accept()
    print("Received connection from", a)  # Remove after debug
    data = "Hello %s" %a[0]  # Remove after debug
    c.send(data.encode())  # Remove after debug
    TheMainGame()

服务器shell中的错误与第19行有关,这就是服务器尝试向客户端询问问题的原因,这也是第二次尝试向客户端发送数据但尝试从未成功发生的原因,这就是为什么我认为套接字正在关闭,即使在此之前我从未告诉它,因为它甚至无法检查是否t == x。什么导致套接字关闭?

1 个答案:

答案 0 :(得分:1)

你在客户端误用全局。

在主体中有global connected。这是不正确的。

相反,在文件顶部放置:

import socket
connected = False

然后,在Connect函数中,引入全局范围变量:

def Connect():
    global connected
    ...

关键字global是告诉函数使用全局范围内的变量,而不是定义它们。

注意: 此代码中还有其他错误,您需要查看并解决这些错误。

  1. 你在server.py中做了同样的事情。
  2. 您尝试在sGetData函数中引用套接字SendData,但s仅在connect函数中是本地的(意味着连接将丢失)一旦功能存在。
相关问题