From b4cd85d8e7ce27f6b2bba23bead58bf0ce9f7319 Mon Sep 17 00:00:00 2001 From: sundayScoop <87186823+sundayScoop@users.noreply.github.com> Date: Thu, 10 Mar 2022 18:44:56 +1000 Subject: [PATCH] Added threading Threading allows for synchronous sending and receiving of messages between client and server. Version is now displayed as the program starts. --- client.py | 44 +++++++++++++++++++++++++++++--------------- server.py | 45 +++++++++++++++++++++++++++++---------------- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/client.py b/client.py index 88e09a4..4f86d38 100644 --- a/client.py +++ b/client.py @@ -1,31 +1,45 @@ ''' -EthanGo v1.0 +EthanGo v1.1 -Super simple socket program. No security. No privacy. -Do not run for prolonged periods of time. - -Issues: -- Each sender must take turns sending messages -- Client will crash if server not up -- Simple terminal chat is not cool +Made for educational purposes only. Written by Julio Medeiros 10/03/2022 ''' import socket +import threading -version = 1.0 +version = 1.1 +print("Version: ", version, "\n") HOST = "127.0.0.1" #input("Server's IP adress: ") # The server's hostname or IP address PORT = 14099 # The port used by the server +def receive(conn): + while True: + data = conn.recv(1024) ## Receive messages from client + if not data: ## If data is null, connection broken > END + break + print("\nThem> ", data.decode("utf-8") ) ## If data exists, decode and display + quit_flag = True + +def send(conn): + while True: + msg = bytes(input("Me> "), "utf-8") ## Collect msg to send from server in bytes + conn.sendall(msg) ## Send msg to client + +quit_flag = False + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) -while True: - msg = bytes(input("Me> "), "utf-8") ## Get msg to send from user and convert into bytes - s.sendall(msg) ## Send message to server - data = s.recv(1024) ## Receive any messages sent from server - print("Them> ", data.decode("utf-8") ) ## Decode messages sent from server -print(f"Received {data!r}") \ No newline at end of file +r_thread = threading.Thread(target=receive, args=(s,), daemon=True) +r_thread.start() + +s_thread = threading.Thread(target=send, args=(s,), daemon=True) +s_thread.start() + +while not quit_flag: + a=0 +quit() \ No newline at end of file diff --git a/server.py b/server.py index 1983c9b..d9715d1 100644 --- a/server.py +++ b/server.py @@ -1,35 +1,48 @@ ''' -EthanGo v1.0 +EthanGo v1.1 -Super simple socket program. No security. No privacy. -Do not run for prolonged periods of time. - -Issues: -- Each sender must take turns sending messages -- Client will crash if server not up -- Simple terminal chat is not cool +Made for educational purposes only. Written by Julio Medeiros 10/03/2022 ''' import socket +import threading -version = 1.0 +version = 1.1 +print("Version: ", version, "\n") HOST = "127.0.0.1" # Standard loopback interface address (localhost) PORT = 14099 # High port to evade scans -s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -s.bind((HOST, PORT)) -s.listen() -conn, addr = s.accept() -with conn: - print(f"Connected by {addr}") +def receive(conn): while True: data = conn.recv(1024) ## Receive messages from client if not data: ## If data is null, connection broken > END break - print("Them> ", data.decode("utf-8") ) ## If data exists, decode and display + print("\nThem> ", data.decode("utf-8") ) ## If data exists, decode and display + quit_flag = True + +def send(conn): + while True: msg = bytes(input("Me> "), "utf-8") ## Collect msg to send from server in bytes conn.sendall(msg) ## Send msg to client + +quit_flag = False + +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.bind((HOST, PORT)) +s.listen() +conn, addr = s.accept() + +r_thread = threading.Thread(target=receive, args=(conn,), daemon=True) +r_thread.start() + +s_thread = threading.Thread(target=send, args=(conn,), daemon=True) +s_thread.start() + + +while not quit_flag: + a=0 +quit()