From a6e8d868d1dfc43f6e8da582e147d82b61ebd657 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:03:03 +0100 Subject: [PATCH] Read messages from server --- src/main/java/ChatClient.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index ce66879..0179d4c 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -9,6 +9,7 @@ public class ChatClient extends JFrame implements KeyListener { private String address; private int port; private Socket connectionToServer; + private BufferedReader fromServerReader; private PrintWriter toServerWriter; // GUI @@ -44,8 +45,15 @@ public class ChatClient extends JFrame implements KeyListener { private void receiveMessages() { try { connectionToServer = new Socket(address, port); + fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); + initGui(); + + while (true) { + String message = fromServerReader.readLine(); + outputTextArea.append(message + "\n"); + } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); }