https://www.cnblogs.com/xiao-apple36/p/9279108.html
#server.py
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.decode('utf-8'))
# server.sendto(data.upper(),client_addr)
server.sendto(data,client_addr)
server.close()
#client.py
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)
# client.sendto(msg.encode(),ip_port)
data,server_addr = client.recvfrom(BUFSIZE)
print('客户端recvfrom ',data.decode('utf-8'),server_addr)
client.close()