服务端代码
import socket phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) phone.bind(('127.0.0.1',8000)) phone.listen(5) while True: conn,client=phone.accept() while True: try: data=conn.recv(1024) print('客户端数据: ', data) conn.send(data.upper()) except ConnectionResetError: break conn.close() phone.close()
客户端代码:
import socket phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) phone.connect(('127.0.0.1',8000)) while True: msg=input('>>:').strip() phone.send(msg.encode('utf-8')) data=phone.recv(1024) print(data.decode('utf-8')) phone.close()