If you never chase your dream, you will never catch them.
若不去追逐梦想,你将永远无法抓住梦想
esp32代码
import socket# 创建 UDP 套接字udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)# 设置目标 IP 和端口IP = ("192.168.31.120", 6666)# 要发送的消息msg = "我是 ESP32 客户端"# 发送消息udp_socket.sendto(msg.encode("utf-8"), IP)# 尝试接收数据try:data, ip = udp_socket.recvfrom(128)print("收到的数据:", data.decode("utf-8"), ip)except socket.timeout:print("超时未收到数据。")finally:# 关闭套接字udp_socket.close()import socket # 创建 UDP 套接字 udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 设置目标 IP 和端口 IP = ("192.168.31.120", 6666) # 要发送的消息 msg = "我是 ESP32 客户端" # 发送消息 udp_socket.sendto(msg.encode("utf-8"), IP) # 尝试接收数据 try: data, ip = udp_socket.recvfrom(128) print("收到的数据:", data.decode("utf-8"), ip) except socket.timeout: print("超时未收到数据。") finally: # 关闭套接字 udp_socket.close()import socket # 创建 UDP 套接字 udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 设置目标 IP 和端口 IP = ("192.168.31.120", 6666) # 要发送的消息 msg = "我是 ESP32 客户端" # 发送消息 udp_socket.sendto(msg.encode("utf-8"), IP) # 尝试接收数据 try: data, ip = udp_socket.recvfrom(128) print("收到的数据:", data.decode("utf-8"), ip) except socket.timeout: print("超时未收到数据。") finally: # 关闭套接字 udp_socket.close()
服务端代码
import socketudp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)IP =("192.168.31.120",6666)udp.bind(IP)while True:data,ip=udp.recvfrom(128)print("接收到的数据:",data.decode("utf-8"),ip)msg ="你好,我是服务器"udp.sendto(msg.encode("utf-8"),ip)import socket udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) IP =("192.168.31.120",6666) udp.bind(IP) while True: data,ip=udp.recvfrom(128) print("接收到的数据:",data.decode("utf-8"),ip) msg ="你好,我是服务器" udp.sendto(msg.encode("utf-8"),ip)import socket udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) IP =("192.168.31.120",6666) udp.bind(IP) while True: data,ip=udp.recvfrom(128) print("接收到的数据:",data.decode("utf-8"),ip) msg ="你好,我是服务器" udp.sendto(msg.encode("utf-8"),ip)
© 版权声明
THE END
- 最新
- 最热
只看作者