From afb3a19d98b3e60aeab164b9dfa3a4627a9a6c68 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Thu, 8 Feb 2024 00:55:46 +0100 Subject: [PATCH 1/3] Client-Counter --- .classpath | 81 ++++++++++++++++++----------------- src/main/java/ChatServer.java | 3 ++ 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/.classpath b/.classpath index 8d95b91..acc4f10 100644 --- a/.classpath +++ b/.classpath @@ -1,40 +1,41 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 0c60774..e9cfb35 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -56,5 +56,8 @@ public class ChatServer { public void removeClient(ClientHandler client) { clients.remove(client); } + public List getClients() { + return clients; + } } \ No newline at end of file From 5f094aef5657e53ff0505f0c969598f96aebcef6 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Thu, 8 Feb 2024 01:53:32 +0100 Subject: [PATCH 2/3] Username Implementation for ClientHandler --- src/main/java/ClientHandler.java | 6 ++++-- user.json | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index fa4b924..d443891 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -1,6 +1,8 @@ import java.io.*; import java.net.Socket; +import javax.swing.JOptionPane; + public class ClientHandler implements Runnable { private ChatServer chatServer; private Socket connectionToClient; @@ -12,7 +14,7 @@ public class ClientHandler implements Runnable { public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; - name = connectionToClient.getInetAddress().getHostAddress(); // Use the client's IP address as their name for simplicity + name = JOptionPane.showInputDialog("welcher Benutzername?"); new Thread(this).start();} // Start a new thread for this client handler @@ -62,4 +64,4 @@ public class ClientHandler implements Runnable { toClientWriter.println(message); // Send the message to the client toClientWriter.flush(); // Flush the stream } -} +} diff --git a/user.json b/user.json index 0201a67..5cbff9f 100644 --- a/user.json +++ b/user.json @@ -10,5 +10,11 @@ "userName": "asd1", "password": "123456", "birthday": "1" + }, + { + "id": "228bf4df-c730-4cd9-9d1c-2446607066aa", + "userName": "jo", + "password": "sieben", + "birthday": "jo" } ] \ No newline at end of file From b762e825e9f19b38b4a8daca5f58c3321f706426 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Thu, 8 Feb 2024 14:50:42 +0100 Subject: [PATCH 3/3] Benutzername abfrage modifiziert --- src/main/java/ChatServer.java | 8 ++++---- src/main/java/ClientHandler.java | 2 +- user.json | 6 ------ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index e9cfb35..f43051b 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -39,10 +39,10 @@ public class ChatServer { } // Methode, um eine Nachricht an alle verbundenen Clients zu senden public void broadcastMessage(String message) { - System.out.println(message); - if (message != null) { - for (ClientHandler client : clients) { - client.sendMessage(message); // Nachricht an jeden Client senden + System.out.println(message); + if (message != null) { + for (ClientHandler client : clients) { + client.sendMessage(message); } } diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index d443891..edba43c 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -14,7 +14,7 @@ public class ClientHandler implements Runnable { public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; - name = JOptionPane.showInputDialog("welcher Benutzername?"); + name = JOptionPane.showInputDialog("Benutzername für neuen Client vergeben"); new Thread(this).start();} // Start a new thread for this client handler diff --git a/user.json b/user.json index 5cbff9f..0201a67 100644 --- a/user.json +++ b/user.json @@ -10,11 +10,5 @@ "userName": "asd1", "password": "123456", "birthday": "1" - }, - { - "id": "228bf4df-c730-4cd9-9d1c-2446607066aa", - "userName": "jo", - "password": "sieben", - "birthday": "jo" } ] \ No newline at end of file