问题
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