client and server

client.py

encoding: utf-8

"""
@software: PyCharm
@Date : 2021/12/1 14:55
@Author : lizhiyuan@hesaitech.com
@FileName: client_.PY
description:
"""
import socket
host = '198.18.32.1'
port = 13400

ip_port=('127.0.0.1',13400)

ip_port=(host,port)
BUFSIZE=1024
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.connect_ex(ip_port) #拨电话

while True: #新增通信循环,客户端可以不断发收消息
msg=input('>>: ').strip()
if len(msg) == 0:continue
s.send(msg.encode('utf-8')) #发消息,说话(只能发送字节类型)

feedback=s.recv(BUFSIZE)                           #收消息,听话
print(feedback.decode('utf-8'))

s.close() #挂电话

server.py

encoding: utf-8

"""
@software: PyCharm
@Date : 2021/12/1 14:54
@Author : lizhiyuan@hesaitech.com
@FileName: server_.PY
description:
"""
import socket

ip_port = ('127.0.0.1',13400) #电话卡

ip_port = ('198.18.35.25',13400) #电话卡
BUFSIZE=1024
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #买手机

s.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE,1) #就是它,在bind前加

s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

s.bind(ip_port) #手机插卡
s.listen(5) #手机待机

while True: #新增接收链接循环,可以不停的接电话
conn,addr=s.accept() #手机接电话
print('client addr:',addr)
# print('接到来自%s的电话' %addr[0])
while True: ##新增通信循环,可以不断的通信,收发消息
msg=conn.recv(BUFSIZE) #听消息,听话
if len(msg) == 0:break #如果不加,那么正在链接的客户端突然断开,recv便不再阻塞,死循环发生
print(msg,type(msg))
conn.send(msg.upper()) #发消息,说话
conn.close() #挂电话
s.close() #手机关机

def doip_test_server(x:UDSClient):

retry = 100

while retry > 0:

try:

# x.server_addr = 0x1306

# x.uds_send_10_service(b'\x03')

ret = x._data_receive()

x._print_hex(ret)

# x._sock, addr = s.accept()

except Exception as e:

print(e)

time.sleep(3)

retry -= 1

posted @ 2022-07-13 11:30  隐士无双  阅读(55)  评论(0)    收藏  举报