Rework client and server quit & exception
This commit is contained in:
parent
6e47a44184
commit
dffb558d9e
3 changed files with 50 additions and 25 deletions
|
@ -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__":
|
||||
|
|
59
gui.py
59
gui.py
|
@ -1,13 +1,23 @@
|
|||
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
|
||||
|
||||
|
||||
|
@ -16,31 +26,49 @@ 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:
|
||||
try:
|
||||
msg = client.receive_server()
|
||||
if msg.lower() == "quit":
|
||||
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):
|
||||
try:
|
||||
client.send_server(message.get())
|
||||
if message.get().lower() == "quit":
|
||||
tchat.destroy()
|
||||
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()
|
||||
login.title("Login")
|
||||
host = StringVar()
|
||||
|
@ -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()
|
||||
|
|
|
@ -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:
|
||||
|
|
Reference in a new issue