我有一个关于套接字的问题,它能够正常运行,但是当我关闭客户端或关闭客户端窗口时,服务器会失去连接(服务器需要保持打开状态并等待其他连接)
while True:
rlist, wlist, xlist = select.select([server_socket]+open_client_sockets, open_client_sockets, [])
for current_socket in rlist:
if current_socket is server_socket:
(new_socket, address) = server_socket.accept()
open_client_sockets.append(new_socket)
print 'new member : ' + str(address)
else:
data = current_socket.recv(1024)
print data
if data == "":
open_client_sockets.remove(current_socket)
print 'Connection with client closed.'
else:
send_messages(data)
问题出在这部分 -
if data == "":
open_client_sockets.remove(current_socket)
print 'Connection with client closed.
以下是错误信息 -
data = current_socket.recv(1024)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
我之前的套接字中没有遇到这个错误。
socket.error
应该就足够了。实际的错误在Linux和Windows之间有所不同(10054表示你在Windows上),但检查10054和54并重新引发其他错误应该就可以解决问题了。 - tdelaney