def recv_msg(): print("连接成功!现在可以接收消息!\n") while True: # 这里发现另一端在send_msg中关闭套接字后会报错。。。于是用try捕获报错信息并正常退出 try: response = sever.recv(1024) recv_time = get.get_time() print("{} 接收到信息:{}".format(recv_time, response.decode("gbk"))) except ConnectionResetError: print("对方已经退出聊天") s.close() break os._exit(0)
def recv_send(self, sock, addr): while True: try: # 测试后发现,当用户率先选择退出时,这边就会报ConnectionResetError response = sock.recv(4096).decode("gbk") msg = "{}用户{}发来消息:{}".format(get.get_time(), addr, response) for client in self.users.values(): client.send(msg.encode("gbk")) except ConnectionResetError: print("用户{}已经退出聊天!".format(addr)) self.users.pop(addr) break
def recv_send(self,sock,addr) : while True : try : response = sock.recv(4096).decode('gbk') msg = '{}用户{}发来消息'.format(get.get_time(),addr,response) for client in self.users.values() : client.send(msg.encode('gbk')) except ConnectionResetError : print('用户{}已经退出聊天!'.format(addr)) self.users.pop(addr) break
def recv_msg(self, s, a): while True: try: resp = s.recv(1024).decode("utf-8") print("{}\n用户 {}:{}".format(get.get_time(), a, resp)) for user in self.users.values(): user.send(resp.encode("utf-8")) except ConnectionResetError: print("用户{}断开连接".format(a)) self.users.pop(a) break except TypeError: print("接收信息失败!")
def recv_send(self, sock, addr): ''' 接收用户信息 :param sock:用户的socket :param addr:用户的ip以及端口 :return: ''' while True: try: # 测试后发现,当用户率先选择退出时,这边就会报ConnectionResetError response = sock.recv(4096).decode("utf-8") msg = "{} 来自用户{}的消息:{}".format(get.get_time(), addr, response) for client in self.users.values(): client.send(msg.encode("utf-8")) except ConnectionResetError: print("用户{}已经退出聊天!".format(addr)) self.users.pop(addr) break
def chat(addr,data): msg = "{} user {} send the message:{}".format(get.get_time(),addr,data) print(msg) for client in users.values(): # what is users.values ? client.send(msg.encode('utf-8'))