diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/Client.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/Client.java index 60f17430aa79eedee49f22b293ff99e75ff368ff..a13d436d9a4d29bb3111699c49200c7bea690cfc 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/Client.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/Client.java @@ -22,8 +22,7 @@ public class Client { public String sendMessage(String msg) throws IOException { out.println(msg); - String resp = in.readLine(); - return resp; + return in.readLine(); } public void stopConnection() throws IOException { diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/ConnectionHandling.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/ConnectionHandling.java index 804c6fef06093a095afb44198377632ee1f0f048..880302995276e944b703c0d13d884068df540d90 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/ConnectionHandling.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/ConnectionHandling.java @@ -10,9 +10,5 @@ public class ConnectionHandling { String resp1 = client.sendMessage("hello"); System.out.println(resp1); - Client client1 = new Client(); - client1.startConnection("localhost", 4444); - String resp2 = client1.sendMessage("hello"); - System.out.println(resp2); } } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/Server.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/Server.java index 1ebd2d287173c77bf821d5682ed2f74ecf29a262..6d82c6033168cdd344739f1969a2528c7856b7b4 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/Server.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestServer/Server.java @@ -25,6 +25,8 @@ public class Server { serverSocket.close(); } + // EchoClientHandler class in the Server + private static class EchoClientHandler extends Thread { private Socket clientSocket; private PrintWriter out; @@ -37,49 +39,43 @@ public class Server { public void run() { try { out = new PrintWriter(clientSocket.getOutputStream(), true); - } catch (IOException e) { - throw new RuntimeException(e); - } - try { - in = new BufferedReader( - new InputStreamReader(clientSocket.getInputStream())); - } catch (IOException e) { - throw new RuntimeException(e); - } + in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - String inputLine; + String inputLine; - try { - while ((inputLine = in.readLine()) != null) { + while (clientSocket.isConnected() && (inputLine = in.readLine()) != null) { System.out.println("Received message from client: " + inputLine); - try { - if ((inputLine = in.readLine()) == null) break; - } catch (IOException e) { - throw new RuntimeException(e); - } + if (".".equals(inputLine)) { out.println("bye"); + out.flush(); + System.out.println("Sent response to client: bye"); break; } + out.println(inputLine); out.flush(); + System.out.println("Sent response to client: " + inputLine); } + } catch (SocketException e) { + // Connection reset by client; no need to print a stack trace + System.out.println("Client connection reset."); } catch (IOException e) { - throw new RuntimeException(e); + // Other IOExceptions - log or handle as needed + e.printStackTrace(); + } finally { + try { + in.close(); + out.close(); + clientSocket.close(); + } catch (IOException e) { + // Log the exception or handle as appropriate for your application + e.printStackTrace(); + } } + } - try { - in.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - out.close(); - try { - clientSocket.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } } + }