Skip to content
Snippets Groups Projects
Commit 7f267d83 authored by Katharina's avatar Katharina
Browse files

add left the chat (msg not send...)

parent b7cd16d6
No related branches found
No related tags found
No related merge requests found
...@@ -348,7 +348,7 @@ def listen_client(): ...@@ -348,7 +348,7 @@ def listen_client():
hold_back_queue.append((message_id, decoded_message)) # Add the message to the hold-back queue hold_back_queue.append((message_id, decoded_message)) # Add the message to the hold-back queue
process_hold_back_queue() # Process messages in the hold-back queue process_hold_back_queue() # Process messages in the hold-back queue
except socket.error as e: # Handle socket errors except socket.error as e: # Handle socket errors
print(f"An error occurred: {e}") print(f"An error occurred while listening: {e}")
break break
except KeyboardInterrupt: # Handle server shutdown via keyboard interrupt ????? Funktioniert das?? except KeyboardInterrupt: # Handle server shutdown via keyboard interrupt ????? Funktioniert das??
print("\nShutting down server...") print("\nShutting down server...")
......
...@@ -2,6 +2,7 @@ import socket ...@@ -2,6 +2,7 @@ import socket
import threading import threading
import time import time
import uuid import uuid
import sys
broadcast_ip = '255.255.255.255'#change ip??? #hard coded? broadcast_ip = '255.255.255.255'#change ip??? #hard coded?
broadcast_port = 55555 broadcast_port = 55555
...@@ -57,8 +58,8 @@ def listen_server(): ...@@ -57,8 +58,8 @@ def listen_server():
#print("this decoded msg", decoded_message) #debug #print("this decoded msg", decoded_message) #debug
except socket.error as e: except socket.error as e:
print(f"An error occurred: {e}") print(f"An error occurred while listening: {e}")
break continue
def sender(): def sender():
...@@ -70,18 +71,33 @@ def sender(): ...@@ -70,18 +71,33 @@ def sender():
#client_socket.sendto(just_nickname, (broadcast_ip, broadcast_port)) #client_socket.sendto(just_nickname, (broadcast_ip, broadcast_port))
client_socket.sendto(just_nickname, (broadcast_ip, broadcast_port)) client_socket.sendto(just_nickname, (broadcast_ip, broadcast_port))
#print("is it leader adresse here", leader_address) #print("is it leader adresse here", leader_address)
while True: try:
while True:
#allows the client to send any message #allows the client to send any message
message = input("") message = input("")
#print("send message to", broadcast_port) #print("send message to", broadcast_port)
#checks for whitemarks #checks for whitemarks
if message.strip(): if message.strip():
message_id = str(uuid.uuid4()) message_id = str(uuid.uuid4())
full_message = f"{message_id}: {nickname}: {message}".encode() full_message = f"{message_id}: {nickname}: {message}".encode()
client_socket.sendto(full_message, (broadcast_ip, broadcast_port)) client_socket.sendto(full_message, (broadcast_ip, broadcast_port))
#print("message sended to", broadcast_port) #print("message sended to", broadcast_port)
except KeyboardInterrupt:
# Handle when the user presses Ctrl+C
print(f"\n{nickname} left the chat.")
# Notify others that this client left the chat
message_id = str(uuid.uuid4()) # New message ID
leave_message = f"{nickname} left the chat".encode()
client_socket.sendto(leave_message, (broadcast_ip, broadcast_port))
time.sleep(2)
# Gracefully close the socket
client_socket.close()
sys.exit() # Exit the program
###############main################################# ###############main#################################
if __name__ == "__main__": if __name__ == "__main__":
# Start listener thread # Start listener thread
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment