OSError: [WinError 10056] A connect request was made on an already connected socket

北城以北 提交于 2019-12-14 03:05:04

问题


I want to make the connection automated. Facing the below issue while running the code. tried adding disconnect but it didn't work. The system is getting connected and data is being transferred. once the connection is lost and when the system tries to reconnect I am facing the issue.

import socket,time
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ip = '192.168.xx.x'
port = 4xxx
address = (ip,port)

def connect():
    try:
        while 1:
            print("connecting")
            try:
                client.connect(address)
                print("connected")
            except TimeoutError:
                print("wait for few mins")
                time.sleep(20)
                connect()
            break
    except ConnectionAbortedError:
        print("connection aborted wait for few sec")
        time.sleep(10)
        connect()    

connect()    
try:
    while 1:
        datasent = client.send(b'\x01\x04')
        if datasent:
            print("sent")
            data= client.recv(1024)
            print(data)
            time.sleep(5)
        if  not datasent:
            connect()


except ConnectionResetError:
    client.shutdown(socket.SHUT_RDWR)
    client.close()
    print("wait for 10 sec")
    time.sleep(10)
    connect()



    >Error: Traceback (most recent call last):
    File "C:\Users\User\eclipse-workspace\Data\ckeck\client.py", line 28, in <module>
    datasent = client.send(b'\x01\x04\') 
    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\ckeck\client.py", line 41, in <module>
    connect()
    File "C:\Users\User\eclipse-workspace\Data\ckeck\client.py", line 13, in connect
    client.connect(address)
    OSError: [WinError 10038] An operation was attempted on something that is not a socket

来源:https://stackoverflow.com/questions/52553273/oserror-winerror-10056-a-connect-request-was-made-on-an-already-connected-soc

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