目录
聊天室
-
客户端:
import socket client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) while True: msg=input('请输入信息').strip() client.sendto(msg.encode('utf8'),('192.168.11.2',8011)) data,server_addr=client.recvfrom(1024) print(f'来自{server_addr}的信息',data.decode('utf8'))
-
服务端
import socket server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) server.bind(('192.168.11.22',8001)) while True: data,client_addr=server.recvfrom(1024) print('>>>>>>',data.decode('utf8'),client_addr) msg=input('回复>>>') server.sendto(msg.encode('utf8'),client_addr)
说明:有个小小的bug,客户端必须先收到客户端的信息才能循环到发送代码这部分