首页 > 解决方案 > OSError: [WinError 10056] 在已连接的套接字上发出了连接请求

问题描述

我想让连接自动化。运行代码时遇到以下问题。尝试添加断开连接,但没有奏效。系统正在连接并正在传输数据。一旦连接丢失并且系统尝试重新连接时,我就会面临这个问题。

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

标签: pythonsocketsconnection

解决方案


推荐阅读