diff --git a/Diff.html b/Diff.html new file mode 100644 index 0000000..535b640 --- /dev/null +++ b/Diff.html @@ -0,0 +1,227 @@ + + +
+ +server.py | +formatted_with_black.server.py | +
---|---|
+ + 1 #!/usr/bin/python3 + 2 + 3 import socket + 4 import threading + 5 + 6 HOST = '0.0.0.0' + 7 PORT = 12345 + 8 + 9 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +10 server.bind((HOST, PORT)) +11 clients = [] +12 lock = threading.Lock() +13 +14 +15 def send_to_all(conn, msg): +16 with lock: +17 for client, _ in clients: +18 if client != conn: +19 try: +20 client.sendall(msg.encode('utf-8')) +21 except BrokenPipeError: +22 print("Verbindung verloren.") +23 with lock: +24 clients.remove((conn, nick)) +25 conn.close() +26 break +27 + ---------------------------------------------------------------------------------------------------------------------------------------------------------- +28 def handle_client(conn, addr): +29 nick = conn.recv(1024).decode('utf-8') +30 print(f"Neuer Client verbunden: {addr} ({nick})") +31 send_to_all(conn, f"{nick} hat den Chat betreten.") +32 +33 with lock: +34 clients.append((conn, nick)) +35 +36 while True: +37 try: +38 msg = conn.recv(1024).decode('utf-8') +39 +40 if not msg: +41 break +42 +43 elif msg == "exit": +44 print("Client getrennt:", addr) +45 +-- 19 Zeilen: send_to_all(conn, f"{nick} hat den Chat verlassen")------------------------------------------------------------------------------------------- +64 +65 break +66 +67 +68 def start_server(): +69 server.listen() +70 print(f'Server läuft auf {HOST}:{PORT}') +71 +72 while True: +73 conn, addr = server.accept() +74 threading.Thread(target=handle_client, args=(conn, addr)).start() +75 +76 +77 if __name__ == '__main__': +78 try: +79 start_server() +80 except KeyboardInterrupt: +81 print("Server wird heruntergefahren.") +82 +83 with lock: +84 +-- 5 Zeilen: for client, nick in clients:------------------------------------------------------------------------------------------------------------------ ++ |
+
+ + 1 #!/usr/bin/python3 + 2 + 3 import socket + 4 import threading + 5 + 6 HOST = "0.0.0.0" + 7 PORT = 12345 + 8 + 9 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +10 server.bind((HOST, PORT)) +11 clients = [] +12 lock = threading.Lock() +13 +14 +15 def send_to_all(conn, msg): +16 with lock: +17 for client, _ in clients: +18 if client != conn: +19 try: +20 client.sendall(msg.encode("utf-8")) +21 except BrokenPipeError: +22 print("Verbindung verloren.") +23 with lock: +24 clients.remove((conn, nick)) +25 conn.close() +26 break +27 +28 +29 def handle_client(conn, addr): +30 nick = conn.recv(1024).decode("utf-8") +31 print(f"Neuer Client verbunden: {addr} ({nick})") +32 send_to_all(conn, f"{nick} hat den Chat betreten.") +33 +34 with lock: +35 clients.append((conn, nick)) +36 +37 while True: +38 try: +39 msg = conn.recv(1024).decode("utf-8") +40 +41 if not msg: +42 break +43 +44 elif msg == "exit": +45 print("Client getrennt:", addr) +46 +-- 19 Zeilen: send_to_all(conn, f"{nick} hat den Chat verlassen")------------------------------------------------------------------------------------------- +65 +66 break +67 +68 +69 def start_server(): +70 server.listen() +71 print(f"Server läuft auf {HOST}:{PORT}") +72 +73 while True: +74 conn, addr = server.accept() +75 threading.Thread(target=handle_client, args=(conn, addr)).start() +76 +77 +78 if __name__ == "__main__": +79 try: +80 start_server() +81 except KeyboardInterrupt: +82 print("Server wird heruntergefahren.") +83 +84 with lock: +85 +-- 5 Zeilen: for client, nick in clients:------------------------------------------------------------------------------------------------------------------ ++ |
+