upload
This commit is contained in:
commit
d8c4d6cc79
2 changed files with 122 additions and 0 deletions
47
client.py
Normal file
47
client.py
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
|
||||||
|
# Server-Informationen
|
||||||
|
HOST = '127.0.0.1'
|
||||||
|
PORT = 12345
|
||||||
|
nickname = "michi"
|
||||||
|
|
||||||
|
def receive_message(sock):
|
||||||
|
""" Funktion für das Empfangen von Nachrichten """
|
||||||
|
while True:
|
||||||
|
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)
|
||||||
|
except:
|
||||||
|
# Schließe die Verbindung, wenn ein Fehler auftritt
|
||||||
|
print("Verbindung getrennt.")
|
||||||
|
break
|
||||||
|
|
||||||
|
def start_client():
|
||||||
|
""" Funktion für das Starten des Clients """
|
||||||
|
# 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()
|
||||||
|
|
||||||
|
client.sendall(nickname.encode('utf-8'))
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
# Lese die Eingabe des Benutzers und sende sie an den Server
|
||||||
|
msg = input(">> ")
|
||||||
|
if msg.lower() == "exit":
|
||||||
|
break
|
||||||
|
client.sendall(msg.encode('utf-8'))
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
# Schließe den Client-Socket
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
start_client()
|
75
server.py
Normal file
75
server.py
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
|
||||||
|
# Server-Informationen
|
||||||
|
HOST = '127.0.0.1'
|
||||||
|
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 handle_client(conn, addr):
|
||||||
|
""" Funktion für die Behandlung eines einzelnen Clients """
|
||||||
|
|
||||||
|
# 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
|
||||||
|
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)
|
||||||
|
clients.remove(conn)
|
||||||
|
conn.close()
|
||||||
|
break
|
||||||
|
|
||||||
|
def start_server():
|
||||||
|
""" Funktion für das Starten des Servers """
|
||||||
|
server.listen()
|
||||||
|
print(f'Server läuft auf {HOST}:{PORT}')
|
||||||
|
while True:
|
||||||
|
# Warte auf eingehende Verbindungen
|
||||||
|
conn, addr = server.accept()
|
||||||
|
# Starte einen neuen Thread für jeden Client
|
||||||
|
threading.Thread(target=handle_client, args=(conn, addr)).start()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
try:
|
||||||
|
start_server()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Server wird heruntergefahren.")
|
||||||
|
server.close()
|
||||||
|
|
Loading…
Reference in a new issue