客户端程序错误尝试对非套接字的对象进行操作

时间:2018-10-05 04:32:38

标签: python eclipse client

我在本地计算机上使用客户端程序连接服务器。直到网络出现故障,通信才非常好。网络脱机后,我尝试关闭套接字并重新连接。但是,shutdown()和close()在客户端程序中未按预期工作。到目前为止,我已经在服务器程序而非任何客户端程序中看到了shutdown()和close()。如果有任何解决方法,请帮助我。

    import socket,time
    client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    ip = '192.168.10.3'
    port = 4196
    address = (ip,port)

    def con():
        client.connect(address)
        print("connected")

    def comm():
        while 1:
            try:
                client.send(b'\x01\x04\x00\x00\x00\x02\x71\xcb')
                print("sent")
                data= client.recv(1024)
                print(" ".join("{:02x}".format(byte) for byte in (data)))
                time.sleep(10)
            except ConnectionResetError:
                print("reset error")
                client.shutdown(socket.SHUT_RD)
                client.close()
                time.sleep(20)
                con()


    con()
    comm() 


    Error:
    Traceback (most recent call last):
    File "C:\Users\User\eclipse-workspace\Data\pwr\TCP.py", line 17, in comm
        datasent = client.send(b'\x01\x04\x00\x00\x00\x02\x71\xcb')
        ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
        File "C:\Users\User\eclipse-workspace\Data\pwr\TCP.py", line 30, in <module>
           comm()
        File "C:\Users\User\eclipse-workspace\Data\pwr\TCP.py", line 26, in comm
           con()
        File "C:\Users\User\eclipse-workspace\Data\pwr\TCP.py", line 11, in con
           client.connect(address)
           OSError: [WinError 10038] An operation was attempted on something that is not a socket

1 个答案:

答案 0 :(得分:0)

感谢您解决@KlausD。必须创建一个新的套接字以重新连接。重新运行socket.socket(...),