From ced5aae13b28ae839f63bc613580369fcb3c904b Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Tue, 6 Feb 2024 22:35:43 +0100 Subject: [PATCH 01/11] Send message to server when user presses enter --- src/main/java/ChatClient.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 8d0b776..ce66879 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -2,13 +2,14 @@ import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; -import java.io.IOException; +import java.io.*; import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { private String address; private int port; private Socket connectionToServer; + private PrintWriter toServerWriter; // GUI private JTextArea outputTextArea; @@ -43,8 +44,9 @@ public class ChatClient extends JFrame implements KeyListener { private void receiveMessages() { try { connectionToServer = new Socket(address, port); + toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); initGui(); - } catch (Exception e) { + } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } } @@ -59,7 +61,8 @@ public class ChatClient extends JFrame implements KeyListener { if (e.getKeyCode() == KeyEvent.VK_ENTER) { String message = inputTextField.getText(); if (!message.isEmpty()) { - // Senden die Nachricht an Server, wenn der erstellt wird + toServerWriter.println(message); + toServerWriter.flush(); inputTextField.setText(""); } } From a6e8d868d1dfc43f6e8da582e147d82b61ebd657 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:03:03 +0100 Subject: [PATCH 02/11] 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."); } From a0d94ed9f8e4eae8d7a13c394d1ae36b5ec875d0 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:13:56 +0100 Subject: [PATCH 03/11] Add scrollbar to window --- src/main/java/ChatClient.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 0179d4c..44a0bc6 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -15,6 +15,7 @@ public class ChatClient extends JFrame implements KeyListener { // GUI private JTextArea outputTextArea; private JTextField inputTextField; + private JScrollPane outputScrollPane; public ChatClient(int port) { super("Chat"); @@ -31,11 +32,13 @@ public class ChatClient extends JFrame implements KeyListener { outputTextArea.setEditable(false); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); + outputScrollPane = new JScrollPane(outputTextArea); + inputTextField = new JTextField(); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.addKeyListener(this); - add(outputTextArea, BorderLayout.CENTER); + add(outputScrollPane, BorderLayout.CENTER); add(inputTextField, BorderLayout.SOUTH); setVisible(true); From 6d9cb97cba88ba86b40bbfc35dd4950b65ad049d Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:15:56 +0100 Subject: [PATCH 04/11] Add auto scroll to end --- src/main/java/ChatClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 44a0bc6..2c043b5 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -56,6 +56,7 @@ public class ChatClient extends JFrame implements KeyListener { while (true) { String message = fromServerReader.readLine(); outputTextArea.append(message + "\n"); + outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); From 6bb126c186cb48ccd46aa8915a7b5700f49d9c3b Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:19:17 +0100 Subject: [PATCH 05/11] Close server at the end --- src/main/java/ChatClient.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 2c043b5..ba31d5e 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -60,6 +60,12 @@ public class ChatClient extends JFrame implements KeyListener { } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + } finally { + try { + connectionToServer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } From 848770d030275eedf1b4b28a9bbe40a14353646e Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:22:35 +0100 Subject: [PATCH 06/11] Close reading messages from server --- src/main/java/ChatClient.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index ba31d5e..a42c3ca 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -66,6 +66,13 @@ public class ChatClient extends JFrame implements KeyListener { } catch (IOException e) { e.printStackTrace(); } + + try { + fromServerReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } From 57b1af041325ed068d8e30ead4bc36559096d3bf Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:24:49 +0100 Subject: [PATCH 07/11] Close writing messages to server --- src/main/java/ChatClient.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index a42c3ca..93731f3 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -73,6 +73,8 @@ public class ChatClient extends JFrame implements KeyListener { e.printStackTrace(); } + toServerWriter.close(); + } } From efbf5ad6c8a8775fbe704695a13b378e97ced5d7 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:27:57 +0100 Subject: [PATCH 08/11] Check connection to server before closing --- src/main/java/ChatClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 93731f3..851ef84 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -61,10 +61,12 @@ public class ChatClient extends JFrame implements KeyListener { } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } finally { - try { - connectionToServer.close(); - } catch (IOException e) { - e.printStackTrace(); + if (null != connectionToServer) { + try { + connectionToServer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } try { From 64626610cab8ac85ea64a28c1abb13913e15771c Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:30:27 +0100 Subject: [PATCH 09/11] Avoid closing fromServerReader in case of null --- src/main/java/ChatClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 851ef84..f14ddd0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -69,10 +69,12 @@ public class ChatClient extends JFrame implements KeyListener { } } - try { - fromServerReader.close(); - } catch (IOException e) { - e.printStackTrace(); + if (null != fromServerReader) { + try { + fromServerReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } } toServerWriter.close(); From 7022585850e6d087085dc9ba6669ba69bf1243da Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:31:54 +0100 Subject: [PATCH 10/11] Check for null value before closing toServerWriter --- src/main/java/ChatClient.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index f14ddd0..eb8fd84 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -77,8 +77,10 @@ public class ChatClient extends JFrame implements KeyListener { } } - toServerWriter.close(); - + if(null != toServerWriter) { + toServerWriter.close(); + } + } } From 50b3994180dcb9b96eae34d2f94914e086ba3eb4 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:34:28 +0100 Subject: [PATCH 11/11] Close messaging window if the connection is broken --- src/main/java/ChatClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index eb8fd84..f39bc2f 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -60,6 +60,7 @@ public class ChatClient extends JFrame implements KeyListener { } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + dispose(); } finally { if (null != connectionToServer) { try { @@ -80,7 +81,6 @@ public class ChatClient extends JFrame implements KeyListener { if(null != toServerWriter) { toServerWriter.close(); } - } }