socket-tcp

server
import socket
ip_port=('127.0.0.1',8080);
back_log=5
buffer_size=1024 serv=socket.socket(socket.AF_INET,socket.SOCK_STREAM); serv.bind(ip_port) serv.listen(back_log)
print('服务端开始打印了')
conn,addr=serv.accept() # 开始卡数 print('双向连接',conn)
print('客户端地址',addr) while True:
data=conn.recv(buffer_size) print('客户端发来的消息是',data.decode('utf-8')) conn.send((data.upper())) conn.close() serv.close()

client
import socket;
ip_port=('127.0.0.1',8080);
buffer_size=1024
client_tcp=socket.socket(socket.AF_INET,socket.SOCK_STREAM);
client_tcp.connect(ip_port) while True:
msg=input('>>:').strip()
client_tcp.send(msg.encode('utf-8')) print('客户端已经发送效益') data=client_tcp.recv(buffer_size) print('收到服务端发来的消息',data.decode('utf-8')) client_tcp.close();
上一篇:【struts2】Struts2的系统架构


下一篇:Django的安装配置和开发