只需用while条件判断来实现
服务器端
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import socket server = socket.socket() server.bind((‘127.0.0.1‘,8080)) server.listen() while True: conn,addr = server.accept() while True: data = conn.recv(1024) print(data) if not data: print("Don‘t has data") break conn.send(data) server.close()
客户端
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import socket client = socket.socket() client.connect((‘127.0.0.1‘,8080)) while True: data = input(‘>>>:‘) client.send(data.encode(‘utf-8‘)) data_recv = client.recv(1024) print(data_recv.decode()) client.close()
运行结果
服务器端结果