From 33a9aa7e469ab9546cda18fa9d6673a4f4f24f33 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:13:02 +0100 Subject: [PATCH 01/15] Einbinden von ClientHandler --- src/main/java/ChatServer.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 0fd0416..0327ad7 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -10,10 +10,12 @@ public class ChatServer { try { serverSocket = new ServerSocket(port); System.out.println("Started ChatServer on port " + port); - + + while(true) { Socket connectionToClient = serverSocket.accept(); - + ClientHandler client = new ClientHandler(this, connectionToClient); System.out.println("Accepted new Client"); + } } catch (IOException e) { e.printStackTrace(); } From 134ebf9a05f9e081c2f9e2520c07f0f7fc538031 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:17:09 +0100 Subject: [PATCH 02/15] =?UTF-8?q?hinzuf=C3=BCgen=20der=20ClientList?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatServer.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 0327ad7..e868615 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -2,9 +2,11 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.util.List; public class ChatServer { private ServerSocket serverSocket; + private List clients; public ChatServer(int port) { try { From 7970faab71c0df772809080ffeb61fd512707f7d Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:19:38 +0100 Subject: [PATCH 03/15] copyOnWriteArrayList --- src/main/java/ChatServer.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index e868615..dc94664 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -3,12 +3,15 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; public class ChatServer { private ServerSocket serverSocket; private List clients; public ChatServer(int port) { + clients = new CopyOnWriteArrayList<>(); + try { serverSocket = new ServerSocket(port); System.out.println("Started ChatServer on port " + port); From 18b9ff3b033db7d1f5c443e5e9ab146fb2e0ea96 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:23:31 +0100 Subject: [PATCH 04/15] clients in CopyOnWriteList --- src/main/java/ChatServer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index dc94664..fdf1007 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -19,6 +19,7 @@ public class ChatServer { while(true) { Socket connectionToClient = serverSocket.accept(); ClientHandler client = new ClientHandler(this, connectionToClient); + clients.add(client); System.out.println("Accepted new Client"); } } catch (IOException e) { From 9c2454a8d027057981a5c2e99beac8844b75ed31 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:25:37 +0100 Subject: [PATCH 05/15] =?UTF-8?q?zus=C3=A4tzliche=20Ausgabenachricht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatServer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index fdf1007..c359642 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -17,6 +17,7 @@ public class ChatServer { System.out.println("Started ChatServer on port " + port); while(true) { + System.out.println("waiting for new Client..."); Socket connectionToClient = serverSocket.accept(); ClientHandler client = new ClientHandler(this, connectionToClient); clients.add(client); From 5adf67da98d69bf15023f9980ac742ca0cf397a3 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:28:20 +0100 Subject: [PATCH 06/15] =?UTF-8?q?refactoring:=20Einr=C3=BCcken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatServer.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index c359642..c647fe6 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -11,17 +11,17 @@ public class ChatServer { public ChatServer(int port) { clients = new CopyOnWriteArrayList<>(); - + try { serverSocket = new ServerSocket(port); System.out.println("Started ChatServer on port " + port); - - while(true) { + + while (true) { System.out.println("waiting for new Client..."); - Socket connectionToClient = serverSocket.accept(); - ClientHandler client = new ClientHandler(this, connectionToClient); - clients.add(client); - System.out.println("Accepted new Client"); + Socket connectionToClient = serverSocket.accept(); + ClientHandler client = new ClientHandler(this, connectionToClient); + clients.add(client); + System.out.println("Accepted new Client"); } } catch (IOException e) { e.printStackTrace(); From c15ed63353828500275e90e7eac60d5780599576 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:33:20 +0100 Subject: [PATCH 07/15] Einbinden von sendMessage in server --- src/main/java/ChatServer.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index c647fe6..3ece54e 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -29,7 +29,10 @@ public class ChatServer { } - public void broadcastMessage(String s) { + public void broadcastMessage(String message) { + for (ClientHandler client : clients) { + client.sendMessage(message); + } } From 0398b37b8ff71e1c1af72a03e4b325f28d48afed Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:34:45 +0100 Subject: [PATCH 08/15] Message ausgeben --- src/main/java/ChatServer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 3ece54e..8a65b1a 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -30,6 +30,7 @@ public class ChatServer { } public void broadcastMessage(String message) { + System.out.println(message); for (ClientHandler client : clients) { client.sendMessage(message); } From d90c8f75605e8279ba587b31f6206e2496a614ae Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:41:02 +0100 Subject: [PATCH 09/15] Abfrage ob Nachricht nicht leer ist --- src/main/java/ChatServer.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 8a65b1a..70929e7 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -31,9 +31,11 @@ public class ChatServer { public void broadcastMessage(String message) { System.out.println(message); + if (message != null) { for (ClientHandler client : clients) { client.sendMessage(message); } + } } From 0957069b71314f6de4b8211d81c2446b08c80864 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:46:21 +0100 Subject: [PATCH 10/15] =?UTF-8?q?finally=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatServer.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 70929e7..33d2860 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -25,6 +25,13 @@ public class ChatServer { } } catch (IOException e) { e.printStackTrace(); + } finally { + try { + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } From 18400eb14b49b0f675e3b15e7d6f6b7541a20fd2 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:49:32 +0100 Subject: [PATCH 11/15] fehlendes if-statement --- src/main/java/ChatServer.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 33d2860..84d84af 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -26,11 +26,12 @@ public class ChatServer { } catch (IOException e) { e.printStackTrace(); } finally { + if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); - } + }} } From b70c89951dd2f6245b26c8f7c2b7778061171ab2 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:50:29 +0100 Subject: [PATCH 12/15] refactoring: Formatierung --- src/main/java/ChatServer.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 84d84af..68eafeb 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -27,12 +27,13 @@ public class ChatServer { e.printStackTrace(); } finally { if (serverSocket != null) { - try { - serverSocket.close(); - } catch (IOException e) { - e.printStackTrace(); - }} - + try { + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } } @@ -40,9 +41,9 @@ public class ChatServer { public void broadcastMessage(String message) { System.out.println(message); if (message != null) { - for (ClientHandler client : clients) { - client.sendMessage(message); - } + for (ClientHandler client : clients) { + client.sendMessage(message); + } } } From 97d94c3057de574b1d60491c8a3f0cdff5673b70 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 01:05:26 +0100 Subject: [PATCH 13/15] remove-Methode --- src/main/java/ChatServer.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 68eafeb..92a4497 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -52,5 +52,9 @@ public class ChatServer { new ChatServer(3141); } + + public void removeClient(ClientHandler client) { + clients.remove(client); + } } \ No newline at end of file From 0dd61cb78c1df560314072d1846f79edb11dcc49 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 01:11:08 +0100 Subject: [PATCH 14/15] refactoring: Kommentare --- src/main/java/ChatServer.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 92a4497..b118b63 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -7,10 +7,10 @@ import java.util.concurrent.CopyOnWriteArrayList; public class ChatServer { private ServerSocket serverSocket; - private List clients; + private List clients; // Liste, um alle verbundenen Clients zu verwalten public ChatServer(int port) { - clients = new CopyOnWriteArrayList<>(); + clients = new CopyOnWriteArrayList<>(); // Verwende CopyOnWriteArrayList für die Thread-Sicherheit try { serverSocket = new ServerSocket(port); @@ -18,9 +18,9 @@ public class ChatServer { while (true) { System.out.println("waiting for new Client..."); - Socket connectionToClient = serverSocket.accept(); + Socket connectionToClient = serverSocket.accept(); // Auf Verbindungen von Clients warten ClientHandler client = new ClientHandler(this, connectionToClient); - clients.add(client); + clients.add(client); // Neuen Client zur Liste hinzufügen System.out.println("Accepted new Client"); } } catch (IOException e) { From 1ca86de8689d6b2702c23dbf46abdc27def1c83b Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 01:13:32 +0100 Subject: [PATCH 15/15] refactoring: Kommentare --- src/main/java/ChatServer.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index b118b63..0c60774 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -37,22 +37,22 @@ public class ChatServer { } } - - public void broadcastMessage(String message) { + // 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); + client.sendMessage(message); // Nachricht an jeden Client senden } } } public static void main(String[] args) { - new ChatServer(3141); + new ChatServer(3141); // ChatServer auf Port 3141 starten (eventuell den Port flexibler noch wählen? falls belegt) } - + // Methode, um einen Client aus der Liste der verbundenen Clients zu entfernen public void removeClient(ClientHandler client) { clients.remove(client); }