python模拟QQ聊天室(tcp加多线程)
服务器代码:
from socket import *
from threading import *
s = socket(AF_INET,SOCK_STREAM)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s.bind(('',8080))
s.listen(1024)
def t_send():
cs_tuple = cs_list[-1]
cs = cs_tuple[0]
while 1:
recv_data = cs.recv(1024)
if len(recv_data) <= 0:
cs_list.remove(cs_tuple)
cs.close()
exit()
for i in cs_list:
send_data = '
['+cs_tuple[1]+':'+str(cs_tuple[2])+']: '+recv_data.decode('gb2312')
print(send_data)
i[0].send(send_data.encode('gb2312'))
cs_list = []
while 1:
cs,(user_ip,user_port) = s.accept()
cs_list.append((cs,user_ip,user_port))
t = Thread(target = t_send)
t.start()
客户端代码:
- 由于没有图形界面,很难模拟可以同时发送和接收的功能,所以只能写一个简单的发送客户端
from socket import *
server_ip = '127.0.0.1'
server_port = 8080
send_data = '大家好^-^'
s = socket(2,1)
try:
s.connect((server_ip,server_port))
except:
print('无法连接到服务器')
else:
s.send(send_data.encode('gb2312'))
recv_data = s.recv(1024)
print(recv_data.decode('gb2312'))
s.close()