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 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__":

65
gui.py
View file

@ -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()

View file

@ -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: