QQ聊天服务器端:
# 开发者:Virtuous# 开发版本:1.0# 开发时间: /9/21 13:05from socket import *s=socket(AF_INET,SOCK_STREAM)s.bind(('',8008))s.listen(1)while True:new_s,c_h_p=s.accept()while True:recv_data=new_s.recv(1024)if len(recv_data)>0 : # 客户端没有退出,而且发送数据到服务器print('客户端:',recv_data.decode('utf-8'))if recv_data.decode('utf-8')=='exit':print('客户端已经退出')break# 发送数据给客户端send_data=input("send:")if len(send_data)>0:new_s.send(send_data.encode('utf-8'))new_s.close()s.close()
QQ聊天服务器端:
# 开发者:Virtuous# 开发版本:1.0# 开发时间: /9/21 13:13from socket import *c=socket(AF_INET,SOCK_STREAM)c.connect(('172.20.10.4',8008))while True:send_data=input("send:")if len(send_data)>0:c.send(send_data.encode('utf-8'))if send_data=='exit':c.close()break# 客户端结束服务器返回的内容recv_data=c.recv(1024)print("服务器:",recv_data.decode('utf-8'))c.close()
先启动服务器,再启动客户端,客户端输入 exit 则退出聊天
终端显示: