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 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__":
|
||||||
|
|
65
gui.py
65
gui.py
|
@ -1,14 +1,24 @@
|
||||||
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
|
pass
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
tchat.destroy()
|
||||||
|
except TclError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
|
@ -16,29 +26,47 @@ 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:
|
||||||
msg = client.receive_server()
|
try:
|
||||||
if msg.lower() == "quit":
|
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
|
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):
|
||||||
client.send_server(message.get())
|
try:
|
||||||
if message.get().lower() == "quit":
|
client.send_server(message.get())
|
||||||
tchat.destroy()
|
if not receive_thread.is_alive() or message.get().lower() == "quit":
|
||||||
message.set("")
|
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 = Tk()
|
||||||
|
@ -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()
|
||||||
|
|
|
@ -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:
|
||||||
|
|
Reference in a new issue