今天用python的socket模块实现了一个聊天室的程序
虽然功能比较简单,但是该有的基本功能还是有的
还望大家指点
服务器端程序:
import socket
import threading
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 5550))
sock.listen(5)
print('Server', socket.gethostbyname('localhost'), 'listening ...')
mydict = dict()
mylist = list()
#把whatToSay传给除了exceptNum的所有人
def tellOthers(exceptNum, whatToSay):
for c in mylist:
if c.fileno() != exceptNum :
try:
c.send(whatToSay.encode())
except:
pass
def subThreadIn(myconnection, connNumber):
nickname = myconnection.recv(1024).decode()
mydict[myconnection.fileno()] = ni