所以,我正在制作一个聊天服务器(原创!),我正在使用multithreading
来处理数据发送/接收,以及日志记录和(很快)服务器端命令。 ANYhoo ..我运行服务器,到目前为止一切都很好。经营一个客户,嘿!有用!重新启动客户端,我得到了;
Exception in thread Thread-5:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Users\Kuro\Desktop\PyChat\Server.py", line 51, in run
s.listen(9999)
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
File "C:\Python27\lib\socket.py", line 170, in _dummy
raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor
在服务器上,但它仍然有效。但是,客户端没有错误。再次运行客户端,这种情况发生了;
[Errno 10061] No connection could be made because the target machine actively refused it
。没有服务器输入/输出。
这是服务器代码:
import socket
import threading
import sys
import os
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IP = raw_input("IP to host the server on: ")
Port = 9340
dat = None
s.bind((IP,Port))
class recvdat(threading.Thread):
def __init__(self, TID, TN):
threading.Thread.__init__(self)
self.TID = TID
self.TN = TN
def run(self):
global dat, conn
while True:
try:
dat = conn.recv(1048)
except:
dat = None
s.close()
break
if dat == "/exit" or None:
s.close()
break
else:
print dat
chtlg.write(dat)
dat = None
class senddat(threading.Thread):
def __init__(self, TID, TN):
threading.Thread.__init__(self)
self.TID = TID
self.TN = TN
def run(self):
pass
class acconn(threading.Thread):
def __init__(self, TID, TN):
threading.Thread.__init__(self)
self.TID = TID
self.TN = TN
def run(self):
global conn, act, ACTID
s.listen(9999)
conn, addr = s.accept()
rdt = recvdat(2,"rdt")
ACTID = ACTID+1
act = acconn(ACTID,"act")
act.start()
rdt.start()
#print "Client Connected with IP "+addr
cwd = os.getcwd()
chtlg = open(cwd+"/ChatLog",'a')
ACTID = 0
act = acconn(ACTID,"act")
act.start()
while True:
time.sleep(2.5)
pass
是的,它是多线程程序的非常错误的借口,然而,它非常不完整,因为我被埋没在制作客户端,而且我不使用类或{{1}经常..
任何帮助都将非常感谢!