From ea00021f6cec5c1f99adf37f3ec2fb7a63579844 Mon Sep 17 00:00:00 2001
From: eb093 <eb093@hdm-stuttgart.de>
Date: Thu, 18 Jan 2024 16:13:33 +0100
Subject: [PATCH] Update: changed the while loop in server to a Thread to be
 able to stop the server.

---
 .../battlearena/Model/Multiplayer/Server.java | 34 ++++++++++++-------
 1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java
index f3555e7b..37a84774 100644
--- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java
+++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java
@@ -25,28 +25,38 @@ public class Server {
     private static int[] cords;
     private static int[] returncords = {enemyx,enemyy,playerID};
     private boolean started = false;
-    private static String ipaddress = "localhost";
 
 
-   /* public static void main(String[] args) throws IOException { //main method for testing purposes
+    public static void main(String[] args) throws IOException { //main method for testing purposes
         Server server = new Server();
         log.info("server starting...");
-        server.start(ipaddress, 4444);
-    } */
+        server.start( 4444);
+    }
 
      public void startServer() throws IOException {
         Server server = new Server();
         log.info("server starting...");
-        server.start("localhost",4444);
+        server.start(4444);
     }
 
-    public void start(String host_ip, int port) throws IOException {
+    public void start(int port) throws IOException {
         serverSocket = new ServerSocket(port);
         log.info("server started!");
         started = true;
-        while (true){
-            new ServerHandler(serverSocket.accept()).start();
-        }
+
+        log.info("server accepting Thread starting...");
+        Thread serverThread = new Thread(() -> {
+            while (true) {
+                try {
+                    new ServerHandler(serverSocket.accept()).start();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+        serverThread.start();
+        log.info("requests accepting Thread started!");
+
     }
 
     private static class ServerHandler extends Thread {
@@ -115,7 +125,6 @@ public class Server {
 
                                 //set playerID:
                                 returncords[2] = 2;
-
                             }
                         }
 
@@ -157,7 +166,6 @@ public class Server {
     public boolean isStarted() {
         return started;
     }
-    public static void setIpaddress(String ipaddress) {
-        Server.ipaddress = ipaddress;
-    }
+
+
 }
-- 
GitLab