这里让客户端给服务端发送的数据被服务端自动发回来
客户端:
import socketclient_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)while True: client_socket.sendto(input('请输入要回显的数据:').encode(), ('127.0.0.1', 23234)) client_recv_temp_data = client_socket.recvfrom(1024) print('回显数据是:', client_recv_temp_data[0].decode())
服务端:
服务端这里不需要编码或者解码,直接回发就行
import socketsocket_serve = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)addr = ('',23234)socket_serve.bind(addr)while True: recv_temp = socket_serve.recvfrom(1024) socket_serve.sendto(recv_temp[0], recv_temp[1])