# ### udp 循环发消息 服务端 import socket sk = socket.socket(type=socket.SOCK_DGRAM) # 绑定地址(在网络上注册该主机,让别人找到你) sk.bind( ("127.0.0.1",9000) ) while True: msg,cli_addr = sk.recvfrom(1024) print(cli_addr) print(msg.decode("utf-8")) message = input("我要说的话>>") # cli_addr (‘127.0.0.1‘, 58745) sk.sendto(message.encode("utf-8") , cli_addr) # 关闭udp连接 sk.close()
# ### udp 循环发消息 客户端 import socket sk = socket.socket(type=socket.SOCK_DGRAM) while True: # 发送数据 message = input("要发送的消息>>") sk.sendto( message.encode("utf-8") , ("127.0.0.1" , 9000) ) # 接受数据 msg,addr = sk.recvfrom(1024) print(msg.decode("utf-8")) # 关闭udp连接 sk.close()
原文地址:https://www.cnblogs.com/huangjiangyong/p/10960930.html
时间: 2024-10-04 15:39:20