共计 539 个字符,预计需要花费 2 分钟才能阅读完成。
UDP 编程
server.py
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(("10.0.128.226", 8888))
while 1:
# data:接收到客户端传递的数据
# addr:客户端的 IP
data, addr = server.recvfrom(1024)
print("收到来自 %s 的消息:%s"%(addr[0], data.decode("utf-8")))
client.py
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while 1:
data = input()
client.sendto(data.encode("utf-8"), ("10.0.128.226", 8888))
# #不建议使用
# client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# client.connect(("10.0.128.226", 8888))
# while 1:
# data = input()
# client.send(data.encode("utf-8"))
正文完
星哥玩云-微信公众号