socket.error: [Errno 10054] 一个现有的连接被远程主机强行关闭 (python2.7) [英] socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host (python2.7)

查看:33
本文介绍了socket.error: [Errno 10054] 一个现有的连接被远程主机强行关闭 (python2.7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的套接字有问题,它运行良好,但是当我关闭客户端/关闭客户端窗口时,服务器失去了连接(服务器需要保持打开状态并等待其他连接)

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)

问题出在这部分 -

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

我在以前的套接字中没有收到此错误

I didn't get this error in my previous socket

推荐答案

当客户端正常关闭套接字时,例如 client_socket.shutdown(socket.SHUT_WR),服务器将接收所有数据然后它的下一个 recv 调用将获得 0 字节.你已经为这个案例编码了.

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.

当客户端在没有正常关闭的情况下退出时,底层套接字实现将执行不正常终止,包括向服务器发送 RESET.在这种情况下,服务器会收到您所看到的异常.这意味着在套接字级别无法保证服务器收到所有数据.

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.

这篇关于socket.error: [Errno 10054] 一个现有的连接被远程主机强行关闭 (python2.7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆