From dffb558d9e27d0f6370dab8c79a01125e4e7471b Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 22 Jul 2019 15:09:11 +0200 Subject: [PATCH] Rework client and server quit & exception --- client.py | 5 ----- gui.py | 65 ++++++++++++++++++++++++++++++++++++++++--------------- server.py | 5 +++-- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/client.py b/client.py index 74174e9..b215471 100644 --- a/client.py +++ b/client.py @@ -1,6 +1,5 @@ from threading import Thread from SecureSocketService import Socket -from sys import exit from socket import error as socket_error @@ -8,7 +7,6 @@ class Client(Socket): def __init__(self, host: str, port: int, service_id: int = 2): super().__init__() self.service_id = service_id - print("Connecting...") self.connect_server(host, port) def receive_server(self): @@ -49,9 +47,6 @@ class Client(Socket): self.socket.close() except socket_error: pass - finally: - print("Disconnected") - exit() if __name__ == "__main__": diff --git a/gui.py b/gui.py index 79bafc1..18f2c7c 100644 --- a/gui.py +++ b/gui.py @@ -1,14 +1,24 @@ -from tkinter import Tk, Frame, Scrollbar, Label, Text, Button, Entry, StringVar, IntVar -from tkinter.messagebox import showerror +from tkinter import Tk, Frame, Scrollbar, Label, Text, Button, Entry, StringVar, IntVar, TclError +from tkinter.messagebox import showerror, showwarning from client import Client from threading import Thread +from socket import error as socket_error + +destroy = False def on_closing(): + global destroy + destroy = True try: - client.quit() - except NameError: + client.send_server("quit") + except TclError: pass + finally: + try: + tchat.destroy() + except TclError: + pass def start(): @@ -16,29 +26,47 @@ def start(): try: global client client = Client(host.get(), port.get()) - except ConnectionError: - showerror("Error", "can't connect to server !") + except (socket_error, ConnectionError): + showerror("Error", "Can't connect to server !") else: login.destroy() def receive(): while True: - msg = client.receive_server() - if msg.lower() == "quit": + try: + msg = client.receive_server() + if msg.lower() == "quit" or not msg: + raise ConnectionError("Client quit") + except (socket_error, ConnectionError, AttributeError): + show_message("""}------------------------------{ +/!\\ [Receive system offline] /!\\ +Press Enter to exit +}------------------------------{""") break - if msg[-1:] != "\n": - msg += "\n" - chat_message.configure(state="normal") - chat_message.insert("end", msg) - chat_message.configure(state="disable") + else: + show_message(msg) def send(event=None): - client.send_server(message.get()) - if message.get().lower() == "quit": - tchat.destroy() - message.set("") + try: + client.send_server(message.get()) + if not receive_thread.is_alive() or message.get().lower() == "quit": + raise ConnectionError("Client quit") + except (socket_error, ConnectionError): + showwarning("Disconnected", "Disconnected from server") + on_closing() + else: + message.set("") + + +def show_message(msg): + if msg[-1:] != "\n": + msg += "\n" + if not destroy: + chat_message.configure(state="normal") + chat_message.insert("end", msg) + chat_message.configure(state="disable") login = Tk() @@ -62,7 +90,8 @@ scrollbar = Scrollbar(chat) scrollbar.pack(side="right", fill="y") chat_message = Text(chat, height=15, width=50, yscrollcommand=scrollbar.set, state="disable") chat_message.pack(side="left", fill="both") -Thread(target=receive).start() +receive_thread = Thread(target=receive) +receive_thread.start() entry = Frame(tchat) entry.pack() diff --git a/server.py b/server.py index 5e48fa9..90318a2 100644 --- a/server.py +++ b/server.py @@ -55,8 +55,9 @@ class Server(Socket): while True: try: data = self.receive(self.clients[name]) - assert data.lower() != "quit" - except (socket_error, AssertionError): + if data.lower() == "quit": + raise ConnectionError("Client quit") + except (socket_error, ConnectionError): self.client_quit(name) break else: