This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# 相当于天线,默认目标地址为192.168.49.160,端口号为 8080
import socket
# 1.创建udp对象
sk = socket.socket(type=socket.SOCK_DGRAM)
# 2.在网络中注册该主机(绑定ip和端口号)
sk.bind( ("127.0.0.1",9000) )
while True:
# 3.收发数据的逻辑
"""udp协议下,默认第一次只能接收数据(没有三次握手,不清楚对方的ip和端口号)"""
# 接受数据
msg, addr = sk.recvfrom(1024)
print(msg.decode())
print(addr)
send_data = input("请输入要发送的数据:")
# 发送数据
sk.sendto(send_data.encode(), addr)
# 4.关闭连接
sk.close()