网络编程-socket实现循环通信
server
import socket phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) phone.bind(('127.0.0.1',8080)) phone.listen(5) conn,client_addr=phone.accept() print(client_addr) #通信循环 while True: data=conn.recv(1024) print("Client data:",data) conn.send(data.upper()) conn.close()
client
import socket client=socket.socket(socket.AF_INET,socket.SOCK_STREAM) client.connect(('127.0.0.1',8080)) #通信循环 while True: msg=input('>> ').strip() client.send(msg.encode('utf-8')) data=client.recv(1024) print(data) #关闭连接 client.close()