服务端:
import socket sk = socket.socket(type=socket.SOCK_DGRAM) sk.bind(("127.0.0.1", 8080)) while True: msg, addr = sk.recvfrom(1024) print(msg.decode("utf-8")) send_msg = input(">>>") sk.sendto(send_msg.encode("utf-8"), addr)
客户端:
import socket sk = socket.socket(type=socket.SOCK_DGRAM) server = ("127.0.0.1", 8080) while True: send_msg = input(">>>") if msg.upper() == "Q": break sk.sendto(send_msg.encode("utf-8"), server) msg = sk.recv(1024).decode("utf-8") if msg.upper() == "Q": break print(msg)
标签:DGRAM,udp,utf,socket,通信,send,sk,msg From: https://www.cnblogs.com/moon3496694/p/17142510.html