# -*- coding: utf-8 -*-
"""
Created on Tue Mar 1 14:20:05 2022
@author: xiaohei
"""
import sys
import socket
import threading
from time import sleep
class SockClient(threading.Thread):
def __init__(self,host_ip,host_port):
threading.Thread.__init__(self)
self.running = False
self.sock = socket.socket()
self.sock.settimeout(20)
try:
self.sock.connect((host_ip, host_port))
except socket.error as e:
print("Socket Connect Error:%s" % e)
sys.exit(1)
print("connecct sucess")
self.running = True
self.error_cnt = 0
def run(self):
while self.running:
try:
send_data = '\x12\x34\x56'
self.sock.send(bytes(send_data.encode('utf-8')))
data = self.sock.recv(1024)
if len(data)>0:
self.error_cnt = 0
recv_data = data.hex()
print("recv:",recv_data)
sleep(1)
except socket.error as e:
print ('socket running error:',str(e))
break
print('sockClient thread exit\n')
if __name__ == "__main__":
sock_client = SockClient('172.24.1.61',8110)
sock_client.start()
try:
while True:
sleep(1)
if not sock_client.is_alive():
break
except KeyboardInterrupt:
print ('ctrl+c')
sock_client.running = False
sock_client.join()
print ('exit finally')