Eddited files to comply with part3

This commit is contained in:
2026-04-19 15:27:30 -04:00
parent 03931fddfe
commit a10c3e7abd
3 changed files with 54 additions and 56 deletions
+13 -20
View File
@@ -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()
client_socket.close()