def threaded_client(self, clientsocket, addr): client_id = addr[1] client_handler = ClientHandler(self, clientsocket, addr) # self is the server instance client_handler.run() # inits all the components in client handler object # adds the client handler object to the list of all the clients objects created by this server. # key: client id, value: client handler self.client_handlers[client_id] = client_handler # assumes dict was initialized in class constructor
def client_handler_thread(self, clientsocket, address): # create the client handler client_handler = ClientHandler(self, clientsocket, address) # init the CH client_handler.init() try: # run the main logic client_handler.run() except EOFError as err: client_id = client_handler.client_id print( "(x) Client Handler Thread Error --> Client ({name}:{client_id}) left abruptly" .format(name=self.names[client_id], client_id=client_id)) client_handler.delete_client_data() except Exception as err: print("(x) Client Handler Thread Error --> {err}".format(err=err)) client_handler.delete_client_data()
def client_handler_thread(self, clientsocket, address): """ Sends the client id assigned to this clientsocket and Creates a new ClientHandler object See also ClientHandler Class :param clientsocket: :param address: :return: a client handler object. """ # threading event ready = Event() # create a new client handler object and return it # inits all the components in client handler object # self is the server instance client_handler = ClientHandler(ready, self, clientsocket, address) # adds the client handler object to the list of all the clients objects created by this server. # key: client id, value: client handler # assumes dict was initialized in class constructor self.clients[client_handler.client_id] = client_handler # start client handler engine when it is ready ready.wait() client_handler.run() return client_handler
class Server(object): MAX_NUM_CONN = 10 #keeps 10 clients in queue def __init__(self, host='127.0.0.1', port=12005): """ Class constructor :param host: :param port: """ self.host = host self.port = port # create an INET, STREAMing socket self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.clients = { } # dictionary of clients handlers objects handling clients. format {clientid:client_handler_object} self.chat_room = {} self.chat_owner = {} # TODO: bind the socket to a public host, and a well-known port self.serversocket.bind((self.host, self.port)) def _listen(self): """ Private method that puts the server in listening mode If successful, prints the string "Listening at <ip>/<port>" i.e "Listening at 127.0.0.1/10000" :return: VOID """ try: self.serversocket.listen(self.MAX_NUM_CONN) print("Listening at " + self.host + "/" + str(self.port)) except: self.serversocket.close() def _accept_clients(self): """ Accept new clients :return: VOID """ while True: try: #TODO: Accept a client #TODO: Create a thread of this client using the client_handler_threaded class client, addr = self.serversocket.accept() Thread(target=self.client_handler_thread, args=(client, addr)).start() except: #TODO: Handle exceptions self.serversocket.close() def send(self, clientsocket, data): """ TODO: Serializes the data with pickle, and sends using the accepted client socket. :param clientsocket: :param data: :return: """ data = pickle.dumps(data) clientsocket.send(data) def receive(self, clientsocket, MAX_BUFFER_SIZE=4096): """ TODO: Deserializes the data with pickle :param clientsocket: :param MAX_BUFFER_SIZE: :return: the deserialized data """ raw_data = clientsocket.recv(MAX_BUFFER_SIZE) return pickle.loads(raw_data) def send_client_id(self, clientsocket, id): """ Already implemented for you :param clientsocket: :return: """ clientid = {'clientid': id} self.send(clientsocket, clientid) def client_handler_thread(self, clientsocket, address): """ Sends the client id assigned to this clientsocket and Creates a new ClientHandler object See also ClientHandler Class :param clientsocket: :param address: :return: a client handler object. """ #id = address[1] #self.send_client_id(clientsocket, id) #TODO: create a new client handler object and return it client_id = address[1] self.client = ClientHandler(self, clientsocket, address) self.client.run() self.clients[self.client.client_id] = self.client return self.client def run(self): """ Already implemented for you. Runs this client :return: VOID """ self._listen() self._accept_clients()