Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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 无法理解错误:OSError:[Errno 9]错误的文件描述符_Python_Multithreading_Class_Sockets_Tcp - Fatal编程技术网

Python 无法理解错误:OSError:[Errno 9]错误的文件描述符

Python 无法理解错误:OSError:[Errno 9]错误的文件描述符,python,multithreading,class,sockets,tcp,Python,Multithreading,Class,Sockets,Tcp,经过一些研究,我发现这与联系有关。在连接请求发出之前,套接字已关闭。但是为什么会这样,我不明白 服务器代码中出现错误: import socket import threading import time data = '' # This thread manages the client connections class ClientThread(threading.Thread): def __init__(self,conn,Address): threading

经过一些研究,我发现这与联系有关。在连接请求发出之前,套接字已关闭。但是为什么会这样,我不明白

服务器代码中出现错误:

import socket
import threading
import time

data = ''

# This thread manages the client connections
class ClientThread(threading.Thread):
   def __init__(self,conn,Address):
       threading.Thread.__init__(self)
       self.conn = conn
       print("New connection added", Address)
   def run(self):
       print ("Connection from : ", Address)
       self.conn.send(bytes("You are connected to IIT H",'utf-8'))

# This thread takes care about recieving the contents of the .txt file
class RecieveThread(threading.Thread):       
   def __init__(self,conn,Address):
      threading.Thread.__init__(self)
      self.conn = conn
   def run(self):
       print("This data is from Housekeeping unit: ",Address)
       data = self.conn.recv(1024).decode()
       print("The wastebin attributes are:  ",  data)

host = ''
port = 5000
server_socket = socket.socket()
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((host, port))

print("The IIT-H server is active")
print("Waiting for clients to connect")   

server_socket.listen(4)
conn, Address = server_socket.accept()
ClientThread(conn,Address).start()
RecieveThread(conn,Address).start()

conn.close()
获取行中的错误:

data = self.conn.recv(1024).decode()

请帮助我

您能否澄清为什么希望连接打开?您在最后一行明确关闭了连接,而没有等待线程完成甚至开始运行。很抱歉,您无法理解这个问题。我不希望连接总是打开的。虽然我已经对它进行了编码,这样一旦启动服务器,它就会一直监听。这个完全相同的代码运行得很好,但突然给我带来了这个错误。插座在应该关上之前就关上了。ReceiveThread类中的行运行良好,直到:print(“此数据来自内务管理单元:”,Address)。让我重新表述一下:在线程中使用套接字时,为什么希望套接字处于可读状态?您既不能在关闭的套接字上发送也不能接收,但在启动线程后立即关闭套接字。让我简化一下:删除
conn.close()
。问题是,这个完全相同的代码工作得很好,我对它做了一些更改以测试一件事。那没有成功。所以我解开了所有的变化。在那之后就出现了错误。你能解释一下为什么你希望连接是开放的吗?您在最后一行明确关闭了连接,而没有等待线程完成甚至开始运行。很抱歉,您无法理解这个问题。我不希望连接总是打开的。虽然我已经对它进行了编码,这样一旦启动服务器,它就会一直监听。这个完全相同的代码运行得很好,但突然给我带来了这个错误。插座在应该关上之前就关上了。ReceiveThread类中的行运行良好,直到:print(“此数据来自内务管理单元:”,Address)。让我重新表述一下:在线程中使用套接字时,为什么希望套接字处于可读状态?您既不能在关闭的套接字上发送也不能接收,但在启动线程后立即关闭套接字。让我简化一下:删除
conn.close()
。问题是,这个完全相同的代码工作得很好,我对它做了一些更改以测试一件事。那没有成功。所以我解开了所有的变化。在那之后就出现了错误。