1 # 服务端
2 import socket
3 ip_port = ('127.0.0.1',9999)
4 sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
5 sk.bind(ip_port)
6
7 while True:
8 data,(host,port) = sk.recvfrom(1024)
9 print(data,host,port)
10 sk.sendto(bytes('ok', encoding='utf-8'), (host,port))
11
12
13 #客户端
14 import socket
15 ip_port = ('127.0.0.1',9999)
16
17 sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
18 while True:
19 inp = input('数据:').strip()
20 if inp == 'exit':
21 break
22 sk.sendto(bytes(inp, encoding='utf-8'),ip_port)
23 data = sk.recvfrom(1024)
24 print(data)
25
26 sk.close()