UDP客户端非常简单,只要把数据发送到服务器地址就可以了。
import socket
host = '192.168.4.254'
port = 12345
addr = (host, port)
c = socket.socket(type=socket.SOCK_DGRAM)
while True:
data = input('> ')
if data.strip() == 'quit':
break
c.sendto(data.encode('utf8'), addr)
print(c.recvfrom(1024)[0].decode('utf8'))
# print(c.recvfrom(1024))
c.close()