def udp_server_concurrency(self): while True: recv_msg, recv_addr = self.udp_socket.recvfrom(1024) msg = recv_msg.decode('utf-8') self.msg = '来自IP:{}端口:{}:\n{}\n'.format(recv_addr[0], recv_addr[1], msg) self.signal_write_msg.emit("写入") Constant.parse_receive(self.msg)
def tcp_client_concurrency(self): """ 功能函数,用于TCP客户端创建子线程的方法,阻塞式接收 :return: """ while True: recv_msg = self.tcp_socket.recv(1024) if recv_msg: msg = recv_msg.decode('utf-8') self.msg = '来自IP:{}端口:{}:\n{}\n'.format( self.address[0], self.address[1], msg) self.signal_write_msg.emit("写入") Constant.parse_receive(self.msg) else: self.tcp_socket.close() self.reset() self.msg = '从服务器断开连接\n' self.signal_write_msg.emit("写入") break
def web_server_concurrency(self): """ 功能函数,供创建线程的方法; 使用子线程用于监听并创建连接,使主线程可以继续运行,以免无响应 使用非阻塞式并发用于接收客户端消息,减少系统资源浪费,使软件轻量化 :return:None """ while True: try: self.client_socket, self.client_address = self.tcp_socket.accept( ) except Exception as ret: pass else: self.client_socket.setblocking(False) # 将创建的客户端套接字存入列表 self.client_socket_list.append( (self.client_socket, self.client_address)) self.msg = 'WEB服务端已连接浏览器,IP:%s端口:%s\n' % self.client_address self.signal_write_msg.emit("写入") # 轮询客户端套接字列表,接收数据 for client, address in self.client_socket_list: try: recv_msg = client.recv(1024) except Exception as ret: pass else: if recv_msg: msg = recv_msg.decode('utf-8') msg_lines = msg.splitlines() msg_dir = re.match(r"[^/]+(/[^ ]*)", msg_lines[0]) self.msg_dir = msg_dir.group(1) self.msg = '来自IP:{}端口:{}:\n请求路径:{}\n'.format( address[0], address[1], self.msg_dir) self.signal_write_msg.emit("写入") Constant.parse_receive(self.msg) self.web_client_socket = client self.all_send() else: client.close() self.client_socket_list.remove((client, address))
def tcp_server_concurrency(self): """ 功能函数,供创建线程的方法; 使用子线程用于监听并创建连接,使主线程可以继续运行,以免无响应 使用非阻塞式并发用于接收客户端消息,减少系统资源浪费,使软件轻量化 :return:None """ while True: try: self.client_socket, self.client_address = self.tcp_socket.accept( ) except Exception as ret: pass else: self.client_socket.setblocking(False) # 将创建的客户端套接字存入列表 self.client_socket_list.append( (self.client_socket, self.client_address)) self.msg = 'TCP服务端已连接IP:%s端口:%s\n' % self.client_address self.signal_write_msg.emit("写入") # 轮询客户端套接字列表,接收数据 for client, address in self.client_socket_list: try: recv_msg = client.recv(1024) except Exception as ret: pass else: if recv_msg: msg = recv_msg.decode('utf-8') self.msg = '来自IP:{}端口:{}:\n{}\n'.format( address[0], address[1], msg) self.signal_write_msg.emit("写入") Constant.parse_receive(self.msg) else: client.close() self.client_socket_list.remove((client, address))