server端
import socket BUFSIZE = 1024 ip_port = ('127.0.0.1', 9999) server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # udp协议 server.bind(ip_port) while True: data,client_addr = server.recvfrom(BUFSIZE) print('server收到的数据', data) server.sendto(data.upper(),client_addr) server.close()
client端
import socket BUFSIZE = 1024 client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) while True: msg = input(">> ").strip() ip_port = ('127.0.0.1', 9999) client.sendto(msg.encode('utf-8'),ip_port) data,server_addr = client.recvfrom(BUFSIZE) print('客户端recvfrom ',data,server_addr) client.close()