juu leuft
This commit is contained in:
parent
d8c4d6cc79
commit
d2af9fc1f7
2 changed files with 54 additions and 43 deletions
38
client.py
38
client.py
|
@ -1,20 +1,28 @@
|
|||
import socket
|
||||
import threading
|
||||
from random import randint
|
||||
|
||||
# Server-Informationen
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 12345
|
||||
nickname = "michi"
|
||||
|
||||
nickname = input("username:")
|
||||
if nickname == "":
|
||||
nickname = ["tux","gnu","wilber","xue","puffy"][randint(0,4)]
|
||||
nickname += str(randint(1000,9999))
|
||||
print(f"dein username wurde zu {nickname} gesetzt.")
|
||||
|
||||
run = True
|
||||
def receive_message(sock):
|
||||
""" Funktion für das Empfangen von Nachrichten """
|
||||
while True:
|
||||
global run
|
||||
while run:
|
||||
try:
|
||||
# Empfange die Nachricht des Servers
|
||||
msg = sock.recv(1024).decode('utf-8')
|
||||
if not msg:
|
||||
break
|
||||
print(f"\r>>{msg}\n", end='', flush=True)
|
||||
print(f"\r{msg}\n", end='', flush=True)
|
||||
except:
|
||||
# Schließe die Verbindung, wenn ein Fehler auftritt
|
||||
print("Verbindung getrennt.")
|
||||
|
@ -22,26 +30,30 @@ def receive_message(sock):
|
|||
|
||||
def start_client():
|
||||
""" Funktion für das Starten des Clients """
|
||||
global run
|
||||
# Erstelle den Client-Socket und verbinde ihn mit dem Server
|
||||
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
client.connect((HOST, PORT))
|
||||
|
||||
# Starte einen Thread für das Empfangen von Nachrichten
|
||||
threading.Thread(target=receive_message, args=(client,)).start()
|
||||
threading.Thread(target=receive_message, args=(client,), daemon=True).start()
|
||||
|
||||
client.sendall(nickname.encode('utf-8'))
|
||||
try:
|
||||
while True:
|
||||
# Lese die Eingabe des Benutzers und sende sie an den Server
|
||||
msg = input(">> ")
|
||||
|
||||
while run:
|
||||
try:
|
||||
msg = input(">>")
|
||||
if msg.lower() == "exit":
|
||||
run = False
|
||||
print("BEENDEN")
|
||||
break
|
||||
client.sendall(msg.encode('utf-8'))
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
# Schließe den Client-Socket
|
||||
client.close()
|
||||
except KeyboardInterrupt:
|
||||
run = False
|
||||
print("\rBEENDEN")
|
||||
break
|
||||
except:
|
||||
break
|
||||
|
||||
if __name__ == '__main__':
|
||||
start_client()
|
||||
|
|
59
server.py
59
server.py
|
@ -8,50 +8,50 @@ PORT = 12345
|
|||
# Erstelle den Server-Socket
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server.bind((HOST, PORT))
|
||||
nicks = {}
|
||||
|
||||
# Liste für die Verfolgung der Clients
|
||||
clients = []
|
||||
|
||||
def sendanall(conn, msg):
|
||||
for client in clients:
|
||||
if client != conn:
|
||||
try:
|
||||
client.sendall(msg.encode('utf-8'))
|
||||
except BrokenPipeError:
|
||||
print("pipe error")
|
||||
pass
|
||||
|
||||
def handle_client(conn, addr):
|
||||
""" Funktion für die Behandlung eines einzelnen Clients """
|
||||
|
||||
""" Funktion für das Behandeln eines einzelnen Clients """
|
||||
nick = conn.recv(1024).decode('utf-8')
|
||||
print(f"Neuer Client verbunden: {addr} (Nickname: {nick})")
|
||||
sendanall(conn, f"{nick} hat den Chat betreten")
|
||||
|
||||
# Füge den Client zur Liste hinzu
|
||||
clients.append(conn)
|
||||
#Nick_given = False
|
||||
#while Nick_given == True:
|
||||
# try:
|
||||
# # Empfange den nickname des Clients
|
||||
# msg = conn.recv(1024).decode('utf-8')
|
||||
# if not msg:
|
||||
# break
|
||||
# print(clients)
|
||||
# print(f"Nick von {addr}: {msg}")
|
||||
# global nicks
|
||||
# nicks[addr] = msg
|
||||
# Nick_given = True
|
||||
# except:
|
||||
# # Entferne den Client aus der Liste und schließe die Verbindung
|
||||
# print("Client getrennt:", addr)
|
||||
# clients.remove(conn)
|
||||
# conn.close()
|
||||
# break
|
||||
print("Neuer Client verbunden:", addr)#,nicks[addr])
|
||||
|
||||
while True:
|
||||
try:
|
||||
# Empfange die Nachricht des Clients
|
||||
msg = conn.recv(1024).decode('utf-8')
|
||||
if not msg:
|
||||
break
|
||||
elif msg == "exit":
|
||||
print("Client getrennt:", addr)
|
||||
sendanall(conn, f"{nick} hat den Chat verlassen")
|
||||
clients.remove(conn)
|
||||
conn.close()
|
||||
break
|
||||
print(clients)
|
||||
print(f"Nachricht von {addr}: {msg}")
|
||||
# Sende die Nachricht an alle anderen Clients
|
||||
for client in clients:
|
||||
if client != conn:
|
||||
client.sendall(msg.encode('utf-8'))
|
||||
except:
|
||||
# Entferne den Client aus der Liste und schließe die Verbindung
|
||||
print("Client getrennt:", addr)
|
||||
|
||||
# Überprüfe, ob das Socket noch geöffnet ist, bevor Sie versuchen zu senden
|
||||
if conn.fileno() != -1:
|
||||
sendanall(conn, f"<{nick}>:{msg}")
|
||||
except ConnectionResetError:
|
||||
# Wenn eine Verbindungsreset-Ausnahme auftritt (Client beendet ohne "exit" zu senden)
|
||||
print("Client getrennt (Verbindungsreset):", addr)
|
||||
sendanall(conn, f"{nick} hat den Chat verlassen")
|
||||
clients.remove(conn)
|
||||
conn.close()
|
||||
break
|
||||
|
@ -72,4 +72,3 @@ if __name__ == '__main__':
|
|||
except KeyboardInterrupt:
|
||||
print("Server wird heruntergefahren.")
|
||||
server.close()
|
||||
|
||||
|
|
Loading…
Reference in a new issue