socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host (python2.7)

白昼怎懂夜的黑 提交于 2021-02-09 11:11:58

问题


I have a problem with my socket , it is well functioning but when i close the client / close the client window the server lost the connection ( the server needs to stay open and wait for other connection)

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)

The problem is in this part -

if data == "":
                open_client_sockets.remove(current_socket)
                print 'Connection with client closed.

This is the error -

data = current_socket.recv(1024)
error: [Errno 10054] An existing connection was forcibly closed by the remote host

I didn't get this error in my previous socket


回答1:


When a client does a graceful close of the socket such as client_socket.shutdown(socket.SHUT_WR) the server will receive all data and then its next recv call will get 0 bytes. You've coded for this case.

When the client exits without a graceful shutdown, the underlying socket implementation will do an ungraceful termination which includes sending a RESET to the server. In this case, the server gets the exception you've seen. It means that at the socket level there is no guarantee that the server received all of its data.

You should update your client to be graceful about closing and also decide what your policy should be on ungraceful exit.



来源:https://stackoverflow.com/questions/35542404/socket-error-errno-10054-an-existing-connection-was-forcibly-closed-by-the-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!