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 socket
|
||||||
import threading
|
import threading
|
||||||
|
from random import randint
|
||||||
|
|
||||||
# Server-Informationen
|
# Server-Informationen
|
||||||
HOST = '127.0.0.1'
|
HOST = '127.0.0.1'
|
||||||
PORT = 12345
|
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):
|
def receive_message(sock):
|
||||||
""" Funktion für das Empfangen von Nachrichten """
|
""" Funktion für das Empfangen von Nachrichten """
|
||||||
while True:
|
global run
|
||||||
|
while run:
|
||||||
try:
|
try:
|
||||||
# Empfange die Nachricht des Servers
|
# Empfange die Nachricht des Servers
|
||||||
msg = sock.recv(1024).decode('utf-8')
|
msg = sock.recv(1024).decode('utf-8')
|
||||||
if not msg:
|
if not msg:
|
||||||
break
|
break
|
||||||
print(f"\r>>{msg}\n", end='', flush=True)
|
print(f"\r{msg}\n", end='', flush=True)
|
||||||
except:
|
except:
|
||||||
# Schließe die Verbindung, wenn ein Fehler auftritt
|
# Schließe die Verbindung, wenn ein Fehler auftritt
|
||||||
print("Verbindung getrennt.")
|
print("Verbindung getrennt.")
|
||||||
|
@ -22,26 +30,30 @@ def receive_message(sock):
|
||||||
|
|
||||||
def start_client():
|
def start_client():
|
||||||
""" Funktion für das Starten des Clients """
|
""" Funktion für das Starten des Clients """
|
||||||
|
global run
|
||||||
# Erstelle den Client-Socket und verbinde ihn mit dem Server
|
# Erstelle den Client-Socket und verbinde ihn mit dem Server
|
||||||
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
client.connect((HOST, PORT))
|
client.connect((HOST, PORT))
|
||||||
|
|
||||||
# Starte einen Thread für das Empfangen von Nachrichten
|
# 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'))
|
client.sendall(nickname.encode('utf-8'))
|
||||||
try:
|
|
||||||
while True:
|
while run:
|
||||||
# Lese die Eingabe des Benutzers und sende sie an den Server
|
try:
|
||||||
msg = input(">> ")
|
msg = input(">>")
|
||||||
if msg.lower() == "exit":
|
if msg.lower() == "exit":
|
||||||
|
run = False
|
||||||
|
print("BEENDEN")
|
||||||
break
|
break
|
||||||
client.sendall(msg.encode('utf-8'))
|
client.sendall(msg.encode('utf-8'))
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
run = False
|
||||||
finally:
|
print("\rBEENDEN")
|
||||||
# Schließe den Client-Socket
|
break
|
||||||
client.close()
|
except:
|
||||||
|
break
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
start_client()
|
start_client()
|
||||||
|
|
57
server.py
57
server.py
|
@ -8,50 +8,50 @@ PORT = 12345
|
||||||
# Erstelle den Server-Socket
|
# Erstelle den Server-Socket
|
||||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
server.bind((HOST, PORT))
|
server.bind((HOST, PORT))
|
||||||
nicks = {}
|
|
||||||
|
|
||||||
# Liste für die Verfolgung der Clients
|
# Liste für die Verfolgung der Clients
|
||||||
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):
|
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
|
# Füge den Client zur Liste hinzu
|
||||||
clients.append(conn)
|
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:
|
while True:
|
||||||
try:
|
try:
|
||||||
# Empfange die Nachricht des Clients
|
# Empfange die Nachricht des Clients
|
||||||
msg = conn.recv(1024).decode('utf-8')
|
msg = conn.recv(1024).decode('utf-8')
|
||||||
if not msg:
|
if not msg:
|
||||||
break
|
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(clients)
|
||||||
print(f"Nachricht von {addr}: {msg}")
|
print(f"Nachricht von {addr}: {msg}")
|
||||||
# Sende die Nachricht an alle anderen Clients
|
|
||||||
for client in clients:
|
# Überprüfe, ob das Socket noch geöffnet ist, bevor Sie versuchen zu senden
|
||||||
if client != conn:
|
if conn.fileno() != -1:
|
||||||
client.sendall(msg.encode('utf-8'))
|
sendanall(conn, f"<{nick}>:{msg}")
|
||||||
except:
|
except ConnectionResetError:
|
||||||
# Entferne den Client aus der Liste und schließe die Verbindung
|
# Wenn eine Verbindungsreset-Ausnahme auftritt (Client beendet ohne "exit" zu senden)
|
||||||
print("Client getrennt:", addr)
|
print("Client getrennt (Verbindungsreset):", addr)
|
||||||
|
sendanall(conn, f"{nick} hat den Chat verlassen")
|
||||||
clients.remove(conn)
|
clients.remove(conn)
|
||||||
conn.close()
|
conn.close()
|
||||||
break
|
break
|
||||||
|
@ -72,4 +72,3 @@ if __name__ == '__main__':
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Server wird heruntergefahren.")
|
print("Server wird heruntergefahren.")
|
||||||
server.close()
|
server.close()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue