python socket.error:[Errno 9]错误的文件描述符

Gil*_*ruk 5 python sockets

我想用Python制作服务器和客户端.它应该建立多个连接,一个是服务器向客户端发送内容,另一个是客户端向服务器发送内容的连接.

第一个连接工作正常,但第二个连接崩溃了消息:

socket.error: [Errno 9] Bad file descriptor
Run Code Online (Sandbox Code Playgroud)

这是服务器:

import socket
import errno
import pickle

def Main():
    host = '188.174.233.99'
    port = 66666

    all_text = ['text1', 'text2', 'text3']

    all_description = ['Test \n Description1\n', 'Test \n Description1\n', 'Test \n Description1\n']

    all_images = ['unlock.png', 'unlock.png', 'unlock.png']
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    s.bind((host, port))
    s.listen(1)


    while True:

        c, addr = s.accept()
        c.setblocking(0)

        print "Connection from: " + str(addr)


        command = c.recv(1024)

        if command == 'GIVEALL':
            textstring = pickle.dumps([all_text, all_images, all_description])#verwandelt Liste in String
            c.send(textstring)



        else:
            try:
                new_event = pickle.loads(command)
                print new_event
                caption = new_event[0]
                image = new_event[1]
                describtion = new_event[2]
                city = new_event[3]

            except:
                pass

        try:
            c.close()

            s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        except socket.error as e:
            if e.errno != errno.ECONNRESET:
                raise
            pass

if __name__ == '__main__':
    Main()
Run Code Online (Sandbox Code Playgroud)

这是客户:

import socket import pickle

from kivy.properties import StringProperty
from kivy.properties import NumericProperty
from kivy.properties import ListProperty

class Netclient(object):

    def __init__(self):
        self.s = socket.socket()
        self.texte = []
        self.current = 'All'

        self.ip = '188.174.233.99'
        self.port = 66666

    def giveWid(self):
        print 'give Widgets executed'
        if self.current == 'All':
            self.texte, self.images, self.description = self.sentHOT(self.ip, self.port)

        return self.texte, self.images, self.description

    def sentHOT(self, host, port):

        self.s.connect((host, port))
        self.s.send('GIVEALL')#sends command

        recived_string = self.s.recv(1023)

        more_text = pickle.loads(recived_string)#verwandelt string in liste

        self.s.close()
        print 'closed'
        return more_text[0], more_text[1], more_text[2]


    def add_event(self, caption, image, description, city='Pawonkow'):
        new_event = [caption, image, description, city]
        new_compact_event = pickle.dumps(new_event)

        self.s.connect((self.ip, self.port))
        self.s.send(new_compact_event)

        self.s.close()


    n = Netclient()
    t, i, d = n.giveWid()
    print t
    n.add_event('new', 'new.png', 'ew event', 'Hanau')
Run Code Online (Sandbox Code Playgroud)

mit*_*ghi 5

原因是您正在尝试重新连接已关闭的套接字.您必须创建一个新套接字或重复使用旧套接字,只要它已连接即可.

在方法def sentHOT(...):注释中,行self.s.close()和方法def add_event(...)注释该行self.s.connect((self.ip, self.port))然后应该工作.此外,请看一下本教程,它可以帮助您进行套接字编程.

  • 接得好.我先不明白这个问题.我以为他意味着他同时开始了2个客户.我删除了我的帖子,因为它不是正确的答案. (2认同)