import socket import threading # this function recieves the thread def receive_messages(connection): while True: # the try block makes it not error when it dissconnects try: # this waits for the message from client message = connection.recv(1024).decode() if not message: # if the client closes the connection its empty print("\nClient Disconnected") break except: print("\nConnection closed.") break # the 'flush' fixes the outputs overriding each other in terminal print(f"\nClient: {message}") # the function for def send_messages(connection): while True: # this waits for you to type message = input() if message.lower() == 'quit': connection.close() break connection.send(message.encode()) #create socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #give socket port and address server_socket.bind(("localhost", 12345)) #listen for connections server_socket.listen() print("Server is waiting for connection...") #accept connection conn, addr = server_socket.accept() print("Connected to:", addr) # the two threads for the inputs receive_thread = threading.Thread(target=receive_messages, args=(conn,)) send_thread = threading.Thread(target=send_messages, args=(conn,)) # starts the process receive_thread.start() send_thread.start()