diff --git a/client.py b/client.py index 6b45654..a10c8c4 100644 --- a/client.py +++ b/client.py @@ -2,41 +2,34 @@ import socket import threading -# function for messages from server -def receive_server(s): +# this function is for receiving messages from the server (broadcasted from the other clients) +def receive_messages(s): while True: - # the try block makes it not error when it dissconnects try: message = s.recv(1024).decode() if not message: - print("\nServer closed the connection") + print("\nDisconnected from server.") break - print(f"\nServer: {message}") + # overwrites the current line to keep the UI clean + print(f"\rIncoming: {message}") + print("You: ", end="", flush=True) except: - print("\nDisconnected from server.") + print("\nConnection lost.") break - -#create socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -#connect to server client_socket.connect(("localhost", 12345)) -print("Connected to server.") -print("Type 'quit' to exit.") +print("Connected to Group Chat.") - -# starts the recieving thread -receive_thread = threading.Thread(target=receive_server, args=(client_socket,)) +# thread to listen for messages while we are typing +receive_thread = threading.Thread(target=receive_messages, args=(client_socket,)) receive_thread.start() - -# loop for sending the messages while True: - msg = input() - if msg.lower() == 'quit': + msg = input("You: ") + if msg.lower() == "quit": break client_socket.send(msg.encode()) -#close connection -client_socket.close() \ No newline at end of file +client_socket.close() diff --git a/readme.txt b/readme.txt index b117f25..3e3a6eb 100644 --- a/readme.txt +++ b/readme.txt @@ -1,7 +1,6 @@ How to run code: First run server.py in the terminal -Next run client.py on a seperate terminal +Next run client.py on a seperate terminal for every user you want -Now that you have established a connection you can send messages to eachother until you want to stop -To end the connection type 'quit' on either side to end the connction \ No newline at end of file +Now that you have established a connection you can send messages between the clients until you want to stop diff --git a/server.py b/server.py index 55b8924..e9d8304 100644 --- a/server.py +++ b/server.py @@ -1,53 +1,59 @@ import socket import threading -# this function recieves the thread -def receive_messages(connection): +# list to keep track of all connected client sockets +clients = [] + + +# this function handles communication with a specific client +def handle_client(connection, address): while True: - # the try block makes it not error when it dissconnects try: - # this waits for the message from client + # receives message from this specific client message = connection.recv(1024).decode() if not message: - # if the client closes the connection its empty - print("\nClient Disconnected") break + + print(f"Message from {address}: {message}") + + # broadcasts this message to all OTHER clients + broadcast(message, connection) except: - print("\nConnection closed.") break - - # the 'flush' fixes the outputs overriding each other in terminal - print(f"\nClient: {message}") + + # cleans up when client disconnects + print(f"Client {address} disconnected.") + if connection in clients: + clients.remove(connection) + connection.close() -# 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()) - +# the function to send a message to everyone except the sender +def broadcast(message, sender_connection): + for client in clients: + if client != sender_connection: + try: + client.send(message.encode()) + except: + client.close() + if client in clients: + clients.remove(client) -#create socket +# creates and sets up socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -#give socket port and address +server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind(("localhost", 12345)) -#listen for connections server_socket.listen() -print("Server is waiting for connection...") +print("Server started. Waiting for multiple clients...") -#accept connection -conn, addr = server_socket.accept() -print("Connected to:", addr) +# the main loop to accept new connections +while True: + conn, addr = server_socket.accept() + print(f"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,)) + clients.append(conn) -# starts the process -receive_thread.start() -send_thread.start() \ No newline at end of file + # starts a new thread for every new client + thread = threading.Thread(target=handle_client, args=(conn, addr)) + thread.start()