Archived
1
0
Fork 0

Rework client and server quit & exception

This commit is contained in:
Ethanell 2019-07-22 15:09:11 +02:00
parent 6e47a44184
commit dffb558d9e
3 changed files with 50 additions and 25 deletions

View file

@ -1,6 +1,5 @@
from threading import Thread from threading import Thread
from SecureSocketService import Socket from SecureSocketService import Socket
from sys import exit
from socket import error as socket_error 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): def __init__(self, host: str, port: int, service_id: int = 2):
super().__init__() super().__init__()
self.service_id = service_id self.service_id = service_id
print("Connecting...")
self.connect_server(host, port) self.connect_server(host, port)
def receive_server(self): def receive_server(self):
@ -49,9 +47,6 @@ class Client(Socket):
self.socket.close() self.socket.close()
except socket_error: except socket_error:
pass pass
finally:
print("Disconnected")
exit()
if __name__ == "__main__": if __name__ == "__main__":

59
gui.py
View file

@ -1,13 +1,23 @@
from tkinter import Tk, Frame, Scrollbar, Label, Text, Button, Entry, StringVar, IntVar from tkinter import Tk, Frame, Scrollbar, Label, Text, Button, Entry, StringVar, IntVar, TclError
from tkinter.messagebox import showerror from tkinter.messagebox import showerror, showwarning
from client import Client from client import Client
from threading import Thread from threading import Thread
from socket import error as socket_error
destroy = False
def on_closing(): def on_closing():
global destroy
destroy = True
try: try:
client.quit() client.send_server("quit")
except NameError: except TclError:
pass
finally:
try:
tchat.destroy()
except TclError:
pass pass
@ -16,31 +26,49 @@ def start():
try: try:
global client global client
client = Client(host.get(), port.get()) client = Client(host.get(), port.get())
except ConnectionError: except (socket_error, ConnectionError):
showerror("Error", "can't connect to server !") showerror("Error", "Can't connect to server !")
else: else:
login.destroy() login.destroy()
def receive(): def receive():
while True: while True:
try:
msg = client.receive_server() 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 break
if msg[-1:] != "\n": else:
msg += "\n" show_message(msg)
chat_message.configure(state="normal")
chat_message.insert("end", msg)
chat_message.configure(state="disable")
def send(event=None): def send(event=None):
try:
client.send_server(message.get()) client.send_server(message.get())
if message.get().lower() == "quit": if not receive_thread.is_alive() or message.get().lower() == "quit":
tchat.destroy() raise ConnectionError("Client quit")
except (socket_error, ConnectionError):
showwarning("Disconnected", "Disconnected from server")
on_closing()
else:
message.set("") 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 = Tk()
login.title("Login") login.title("Login")
host = StringVar() host = StringVar()
@ -62,7 +90,8 @@ scrollbar = Scrollbar(chat)
scrollbar.pack(side="right", fill="y") scrollbar.pack(side="right", fill="y")
chat_message = Text(chat, height=15, width=50, yscrollcommand=scrollbar.set, state="disable") chat_message = Text(chat, height=15, width=50, yscrollcommand=scrollbar.set, state="disable")
chat_message.pack(side="left", fill="both") chat_message.pack(side="left", fill="both")
Thread(target=receive).start() receive_thread = Thread(target=receive)
receive_thread.start()
entry = Frame(tchat) entry = Frame(tchat)
entry.pack() entry.pack()

View file

@ -55,8 +55,9 @@ class Server(Socket):
while True: while True:
try: try:
data = self.receive(self.clients[name]) data = self.receive(self.clients[name])
assert data.lower() != "quit" if data.lower() == "quit":
except (socket_error, AssertionError): raise ConnectionError("Client quit")
except (socket_error, ConnectionError):
self.client_quit(name) self.client_quit(name)
break break
else: else: