diff --git a/client.py b/client.py index 77baa2f..74174e9 100644 --- a/client.py +++ b/client.py @@ -10,20 +10,18 @@ class Client(Socket): self.service_id = service_id print("Connecting...") self.connect_server(host, port) - Thread(target=self.receive_server).start() - Thread(target=self.send_input).start() def receive_server(self): - while True: - try: - data = self.receive(self.socket) - except socket_error: + try: + data = self.receive(self.socket) + except socket_error: + self.quit() + return False + else: + if data.lower() == "quit": self.quit() - else: - if data.lower() == "quit": - self.quit() - break - print(data) + return False + return data def send_server(self, data): try: @@ -38,6 +36,14 @@ class Client(Socket): if data.lower() == "quit": break + def receive_print(self): + while True: + message = self.receive_server() + if message: + print(message) + else: + break + def quit(self): try: self.socket.close() @@ -49,4 +55,6 @@ class Client(Socket): if __name__ == "__main__": - Client("localhost", 3621) + client = Client("localhost", 3621) + Thread(target=client.receive_print).start() + Thread(target=client.send_input).start() diff --git a/gui.py b/gui.py new file mode 100644 index 0000000..045c5b3 --- /dev/null +++ b/gui.py @@ -0,0 +1,72 @@ +from tkinter import Tk, Frame, Scrollbar, Label, Text, Button, Entry, StringVar, IntVar, LEFT, RIGHT, BOTH, Y, END +from tkinter.messagebox import showerror +from client import Client +from threading import Thread + + +def on_closing(): + try: + client.quit() + except NameError: + pass + + +def start(): + if host.get() and port.get(): + try: + global client + client = Client(host.get(), port.get()) + except ConnectionError: + showerror("Error", "can't connect tu server !") + else: + login.destroy() + + +def receive(): + while True: + msg = client.receive_server() + if msg.lower() == "quit": + break + if msg[-1:] != "\n": + msg += "\n" + chat_message.insert(END, msg) + + +def send(event=None): + client.send_server(message.get()) + if message.get().lower() == "quit": + tchat.destroy() + message.set("") + + +login = Tk() +login.title("Login") +host = StringVar() +port = IntVar() +Label(login, text="Host & port:").pack() +login_f = Frame(login) +login_f.pack() +Entry(login_f, textvariable=host, width=14).grid(row=0, column=0) +Entry(login_f, textvariable=port, width=4).grid(row=0, column=1) +Button(login, text="Submit", command=start).pack() +login.mainloop() + +tchat = Tk() +tchat.title("PyTchat") +tchat.protocol("WM_DELETE_WINDOW", on_closing) +chat = Frame(tchat) +chat.pack() +scrollbar = Scrollbar(chat) +scrollbar.pack(side=RIGHT, fill=Y) +chat_message = Text(chat, height=15, width=50, yscrollcommand=scrollbar.set) +chat_message.pack(side=LEFT, fill=BOTH) +Thread(target=receive).start() + +entry = Frame(tchat) +entry.pack() +message = StringVar() +field = Entry(entry, textvariable=message) +field.bind("", send) +field.grid(row=0, column=0) +Button(entry, text="Send", command=send).grid(row=0, column=1) +tchat.mainloop() diff --git a/server.py b/server.py index c7f14f9..6941b33 100644 --- a/server.py +++ b/server.py @@ -5,25 +5,29 @@ from socket import error as socket_error class Server(Socket): def __init__(self, host: str = "localhost", port: int = 3621, service_id: int = 2): + print("Server start") super().__init__() self.socket.bind((host, port)) self.socket.listen(5) self.service_id = service_id self.command_suffix = "!" + self.welcom_msg = "Welcom ! Type \"!help\" to see commands and \"quit\" to exit" self.commands = {"help": self.command_help, "players list": self.command_players_list} self.clients = dict() + print("Wait for connexion...") Thread(target=self.connexion).start() def connexion(self): while True: try: c, address = self.connect_client(self.socket) - except socket_error: + except (socket_error, ConnectionError): continue else: name = self.client_name(c) if name: self.clients[name] = c + self.send(self.clients[name], self.welcom_msg) self.broadcast(f"{name} is online !") Thread(target=self.listen_client, args=(name,)).start() @@ -54,13 +58,11 @@ class Server(Socket): break else: if not self.command(data, name): - Thread(target=self.broadcast, args=(f"{name}: {data}", name)).start() + Thread(target=self.broadcast, args=(f"{name}: {data}",)).start() - def broadcast(self, message, author=None): + def broadcast(self, message): print(message) for i in self.clients: - if i == author: - continue try: self.send(self.clients[i], message) except socket_error: