socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host
最近在网络编程开发中遇到socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host这样的错误,查了些资料也没什么用;
最后发现原来是socket server在设计时提前将socket套接字对象关掉了,所以导致下次使用找不到socket server的套接字对象而报错
import socket from threading import Thread import time command=["hostname","ipconfig","systeminfo"] command="###".join(command) print(command) s=socket.socket() socket.setdefaulttimeout(5) s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) addr=("0.0.0.0",9999) s.bind(addr) s.listen(5) print("waiting for connecting.....") def fun(s,c,a): for i in command.split("###"): time.sleep(1) n=c.send(str(i).encode()) print "send command {} to client".format(i),n,"bytes" c.close() #s.close() while True: c,a=s.accept() if c: print("I have enter into windwos ") print a,c t=Thread(target=fun,args=(s,c,a)) t.setDaemon(True) t.start()
注释掉s.close()就可以正常运行,多次调用了
经实验,如果在socket server套接字对象关闭之前调用,程序是可以正常运行的,关闭之后再调用,就会报错了!!!