From aa10ff56c616beea88cd391093074c29f38d411e Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 17:24:02 +0100 Subject: [PATCH 01/24] Create connection to server using socket --- src/main/java/ChatClient.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index c4f2737..3627c91 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,12 +1,31 @@ import javax.swing.*; +import java.io.IOException; +import java.net.Socket; public class ChatClient { private String address; - public ChatClient() { + private int port; + private Socket connectionToServer; + + public ChatClient(int port) { + this.port = port; address = JOptionPane.showInputDialog("bitte IP-Adresse"); + + if (address != null) { + receiveMessages(); + } + } + + private void receiveMessages() { + try { + connectionToServer = new Socket(address, port); + } catch (IOException e) { + JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + } } public static void main(String[] args) { - new ChatClient(); + new ChatClient(3141); + } } \ No newline at end of file From 3072cf2853cdc552f781ecfefb53ec787b588a4b Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 19:51:03 +0100 Subject: [PATCH 02/24] add GUI window for chat --- src/main/java/ChatClient.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 3627c91..5c60638 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -2,12 +2,13 @@ import javax.swing.*; import java.io.IOException; import java.net.Socket; -public class ChatClient { +public class ChatClient extends JFrame { private String address; private int port; private Socket connectionToServer; - + public ChatClient(int port) { + super("Chat"); this.port = port; address = JOptionPane.showInputDialog("bitte IP-Adresse"); @@ -16,10 +17,18 @@ public class ChatClient { } } + private void initGui() { + setVisible(true); + setSize(800, 600); + setDefaultCloseOperation(EXIT_ON_CLOSE); + } private void receiveMessages() { try { - connectionToServer = new Socket(address, port); - } catch (IOException e) { + // Um GUI zu testen + // Kommentar in der Zeile 28 wird weggenohmen, wenn der Server erstellt wird. + // connectionToServer = new Socket(address, port); + initGui(); + } catch (Exception e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } } From 1e3e1ffe4a7ede432bb5411e0818e77dbafd7ba8 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 20:23:33 +0100 Subject: [PATCH 03/24] add input and output areas in GUI window --- src/main/java/ChatClient.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 5c60638..00600d0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -7,6 +7,10 @@ public class ChatClient extends JFrame { private int port; private Socket connectionToServer; + // GUI + private JTextArea outputTextArea; + private JTextField inputTextField; + public ChatClient(int port) { super("Chat"); this.port = port; @@ -18,6 +22,12 @@ public class ChatClient extends JFrame { } private void initGui() { + outputTextArea = new JTextArea(); + inputTextField = new JTextField(); + + add(outputTextArea); + add(inputTextField); + setVisible(true); setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); From 6d32cd126c5df9b2ca9d4b3047a93516cb55eb48 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 21:11:38 +0100 Subject: [PATCH 04/24] Change position of input and output areas --- src/main/java/ChatClient.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 00600d0..6380f66 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,4 +1,5 @@ import javax.swing.*; +import java.awt.*; import java.io.IOException; import java.net.Socket; @@ -23,10 +24,13 @@ public class ChatClient extends JFrame { private void initGui() { outputTextArea = new JTextArea(); + outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); + inputTextField = new JTextField(); + inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); - add(outputTextArea); - add(inputTextField); + add(outputTextArea, BorderLayout.CENTER); + add(inputTextField, BorderLayout.SOUTH); setVisible(true); setSize(800, 600); From 92244c512b47796f38eab6fed55491b818c0a1a6 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 21:14:19 +0100 Subject: [PATCH 05/24] Prevent writing in output area --- 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 6380f66..8f0c1f0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -24,6 +24,7 @@ public class ChatClient extends JFrame { private void initGui() { outputTextArea = new JTextArea(); + outputTextArea.setEditable(false); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); inputTextField = new JTextField(); From 3800aeb4618cd70b201820622e4496dbf8c0fd46 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 21:54:23 +0100 Subject: [PATCH 06/24] Remove text from input field and later send to server after user presses enter --- src/main/java/ChatClient.java | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 8f0c1f0..695f622 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,9 +1,11 @@ import javax.swing.*; import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; import java.io.IOException; import java.net.Socket; -public class ChatClient extends JFrame { +public class ChatClient extends JFrame implements KeyListener { private String address; private int port; private Socket connectionToServer; @@ -29,6 +31,7 @@ public class ChatClient extends JFrame { inputTextField = new JTextField(); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); + inputTextField.addKeyListener(this); add(outputTextArea, BorderLayout.CENTER); add(inputTextField, BorderLayout.SOUTH); @@ -48,8 +51,28 @@ public class ChatClient extends JFrame { } } + @Override + public void keyTyped(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + String message = inputTextField.getText(); + if (!message.isEmpty()) { + // Senden die Nachricht an Server, wenn der erstellt wird + inputTextField.setText(""); + } + } + } + + @Override + public void keyReleased(KeyEvent e) { + + } + public static void main(String[] args) { new ChatClient(3141); - } -} \ No newline at end of file +} From e7565ec663bafd05e6fe75f80541ebacb61a94af Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 2 Feb 2024 17:12:38 +0100 Subject: [PATCH 07/24] class Client Handler --- bin/.project | 23 +++++ bin/README.md | 92 +++++++++++++++++++ bin/build-project.sh | 24 +++++ bin/target/classes/META-INF/MANIFEST.MF | 4 + .../org.progmethoden/java-chat/pom.properties | 7 ++ .../maven/org.progmethoden/java-chat/pom.xml | 6 ++ bin/target/classes/test2 | 0 bin/target/test-classes/test.txt | 0 bin/target/test-classes/text.txt | 0 bin/team.md | 5 + src/main/java/ClientHandler.java | 22 +++++ 11 files changed, 183 insertions(+) create mode 100644 bin/.project create mode 100644 bin/README.md create mode 100644 bin/build-project.sh create mode 100644 bin/target/classes/META-INF/MANIFEST.MF create mode 100644 bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties create mode 100644 bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml create mode 100644 bin/target/classes/test2 create mode 100644 bin/target/test-classes/test.txt create mode 100644 bin/target/test-classes/text.txt create mode 100644 bin/team.md create mode 100644 src/main/java/ClientHandler.java diff --git a/bin/.project b/bin/.project new file mode 100644 index 0000000..7caa276 --- /dev/null +++ b/bin/.project @@ -0,0 +1,23 @@ + + + java-chat + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/bin/README.md b/bin/README.md new file mode 100644 index 0000000..8a88154 --- /dev/null +++ b/bin/README.md @@ -0,0 +1,92 @@ +# Java Chat + + + +## Getting started + +To make it easy for you to get started with GitLab, here's a list of recommended next steps. + +Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! + +## Add your files + +- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files +- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: + +``` +cd existing_repo +git remote add origin https://gitlab.cs.hs-fulda.de/fdai7332/java-chat.git +git branch -M main +git push -uf origin main +``` + +## Integrate with your tools + +- [ ] [Set up project integrations](https://gitlab.cs.hs-fulda.de/fdai7332/java-chat/-/settings/integrations) + +## Collaborate with your team + +- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) +- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) +- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) +- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) +- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) + +## Test and Deploy + +Use the built-in continuous integration in GitLab. + +- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) +- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) +- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) +- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) +- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) + +*** + +# Editing this README + +When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. + +## Suggestions for a good README +Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. + +## Name +Choose a self-explaining name for your project. + +## Description +Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. + +## Badges +On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. + +## Visuals +Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. + +## Installation +Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. + +## Usage +Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. + +## Support +Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. + +## Roadmap +If you have ideas for releases in the future, it is a good idea to list them in the README. + +## Contributing +State if you are open to contributions and what your requirements are for accepting them. + +For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. + +You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. + +## Authors and acknowledgment +Show your appreciation to those who have contributed to the project. + +## License +For open source projects, say how it is licensed. + +## Project status +If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. diff --git a/bin/build-project.sh b/bin/build-project.sh new file mode 100644 index 0000000..ed85686 --- /dev/null +++ b/bin/build-project.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Überprüfen, ob Maven installiert ist +command -v mvn >/dev/null 2>&1 || { echo >&2 "Maven ist erforderlich, aber nicht installiert. Bitte installieren Sie Maven."; exit 1; } + +# Projekt übersetzen und Unittests ausführen +mvn clean compile test + +# Überprüfen, ob der Build erfolgreich war +if [ $? -eq 0 ]; then + echo "Build erfolgreich abgeschlossen." +else + echo "Fehler beim Build. Bitte überprüfen Sie Ihre Codebasis." + exit 1 +fi + + + +remove() { + rm -r build + rm main +} + +remove diff --git a/bin/target/classes/META-INF/MANIFEST.MF b/bin/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 0000000..b087487 --- /dev/null +++ b/bin/target/classes/META-INF/MANIFEST.MF @@ -0,0 +1,4 @@ +Manifest-Version: 1.0 +Build-Jdk-Spec: 17 +Created-By: Maven Integration for Eclipse + diff --git a/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties new file mode 100644 index 0000000..638f58d --- /dev/null +++ b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties @@ -0,0 +1,7 @@ +#Generated by Maven Integration for Eclipse +#Fri Feb 02 12:52:10 CET 2024 +m2e.projectLocation=S\:\\backup\\Studium\\1W\\Programmiermethoden und -Werkzeuge\\javachat\\java-chat +m2e.projectName=java-chat +groupId=org.progmethoden +artifactId=java-chat +version=0.0.1-SNAPSHOT diff --git a/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml new file mode 100644 index 0000000..05188ae --- /dev/null +++ b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml @@ -0,0 +1,6 @@ + + 4.0.0 + org.progmethoden + java-chat + 0.0.1-SNAPSHOT + \ No newline at end of file diff --git a/bin/target/classes/test2 b/bin/target/classes/test2 new file mode 100644 index 0000000..e69de29 diff --git a/bin/target/test-classes/test.txt b/bin/target/test-classes/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/bin/target/test-classes/text.txt b/bin/target/test-classes/text.txt new file mode 100644 index 0000000..e69de29 diff --git a/bin/team.md b/bin/team.md new file mode 100644 index 0000000..1e6866c --- /dev/null +++ b/bin/team.md @@ -0,0 +1,5 @@ +- Valentin Spiroski, fdai7332 +- Richard Schmidt, fdai7420 +- Paul Kattenborn, fdai7599 +- Marc Dimmerling, fdai7579 +- Alena Bandarovich, fdai5595 diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java new file mode 100644 index 0000000..6489bb5 --- /dev/null +++ b/src/main/java/ClientHandler.java @@ -0,0 +1,22 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.ServerSocket; + +public class ClientHandler implements Runnable { + private Server server; + private ServerSocket connectionToClient; + private String name; + private BufferedReader fromClientReader; + private PrintWriter toClientWriter; + + @Override + public void run() { + // TODO Auto-generated method stub + + } + + +} From d0695ade2142c94df38b1aedabf40617646de947 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 2 Feb 2024 17:38:25 +0100 Subject: [PATCH 08/24] Class ClientHandler --- src/main/java/ClientHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 6489bb5..f3f9b3c 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -18,5 +18,5 @@ public class ClientHandler implements Runnable { } - + } From 9dc558b69e646afcdeb951ca2e3a7947154368fe Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 22:58:21 +0100 Subject: [PATCH 09/24] ClientHandler Konstruktor --- .gitignore | 2 +- src/main/java/ClientHandler.java | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5d1bde8..6951c29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /target/ /.idea/ /.settings/ -java-chat.iml \ No newline at end of file +java-chat.iml diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index f3f9b3c..200dccf 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -4,14 +4,27 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; +import java.net.Socket; + public class ClientHandler implements Runnable { - private Server server; - private ServerSocket connectionToClient; + private ChatServer chatServer; + private Socket connectionToClient; private String name; private BufferedReader fromClientReader; private PrintWriter toClientWriter; + + public ClientHandler(ChatServer chatServer, Socket connectionToClient) { + this.chatServer = chatServer; + this.connectionToClient = connectionToClient; + name = connectionToClient.getInetAddress().getHostAddress(); + new Thread(this).start(); + } + + + + @Override public void run() { // TODO Auto-generated method stub From c928d6d4c29e0305f75756add8b555a044f9fb64 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:17:02 +0100 Subject: [PATCH 10/24] run Method, implement from Runnable --- src/main/java/ClientHandler.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 200dccf..6711e84 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -27,8 +27,14 @@ public class ClientHandler implements Runnable { @Override public void run() { - // TODO Auto-generated method stub - + try { + fromClientReader = new BufferedReader(new InputStreamReader(connectionToClient.getInputStream())); + toClientWriter = new PrintWriter(new OutputStreamWriter(connectionToClient.getOutputStream())); + chatServer.broadcastMessage(name + " connected."); + + } catch (IOException e) { + e.printStackTrace(); + } } From 10e138bc22262f629dcf56f5e3df948cf9cf4efb Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:27:42 +0100 Subject: [PATCH 11/24] while loop in run() for waiting for new messages --- src/main/java/ClientHandler.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 6711e84..fe885bd 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -32,6 +32,11 @@ public class ClientHandler implements Runnable { toClientWriter = new PrintWriter(new OutputStreamWriter(connectionToClient.getOutputStream())); chatServer.broadcastMessage(name + " connected."); + String message = fromClientReader.readLine(); + while (message!=null) { + chatServer.broadcastMessage(name + ": " + message); + message = fromClientReader.readLine(); + } } catch (IOException e) { e.printStackTrace(); } From 46231b97286c5ea7a4ad7d9979305b8d01c96400 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:33:12 +0100 Subject: [PATCH 12/24] sendMessage Method --- src/main/java/ClientHandler.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index fe885bd..c59f1ed 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -42,5 +42,9 @@ public class ClientHandler implements Runnable { } } + public void sendMessage(String message) { + toClientWriter.println(message); + toClientWriter.flush(); + } } From cc284a2262a52ed8027b259a61e1a624259d43f1 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:45:06 +0100 Subject: [PATCH 13/24] finally in run() --- src/main/java/ClientHandler.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index c59f1ed..5561e3c 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -39,6 +39,20 @@ public class ClientHandler implements Runnable { } } catch (IOException e) { e.printStackTrace(); + } finally { + //chatServer.removeClient(this); + chatServer.broadcastMessage(name + " disconnected."); + + if (fromClientReader != null) { + try { + fromClientReader.close(); + } catch (IOException e){ + e.printStackTrace(); + } + } + if (toClientWriter != null) { + toClientWriter.close(); + } } } From 9b87159d506bc2d401dcbddf816b693880cc1c00 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:48:42 +0100 Subject: [PATCH 14/24] small correction --- src/main/java/ClientHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 5561e3c..9c52f54 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -41,6 +41,7 @@ public class ClientHandler implements Runnable { e.printStackTrace(); } finally { //chatServer.removeClient(this); + throw new RuntimeException(e); chatServer.broadcastMessage(name + " disconnected."); if (fromClientReader != null) { From af2c1d8b84caf30f63e7a188608926149cbd2322 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:39:27 +0100 Subject: [PATCH 15/24] small correction --- src/main/java/ClientHandler.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 9c52f54..3b30c32 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -1,4 +1,6 @@ + import java.io.BufferedReader; + import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; @@ -38,10 +40,10 @@ public class ClientHandler implements Runnable { message = fromClientReader.readLine(); } } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); + } finally { //chatServer.removeClient(this); - throw new RuntimeException(e); chatServer.broadcastMessage(name + " disconnected."); if (fromClientReader != null) { From 7bc8165582bd6ec0db3a5ab36975d74c10159f29 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:42:48 +0100 Subject: [PATCH 16/24] refactoring: imports --- src/main/java/ClientHandler.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 3b30c32..bbc6455 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -1,14 +1,6 @@ - -import java.io.BufferedReader; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.net.ServerSocket; +import java.io.*; import java.net.Socket; - public class ClientHandler implements Runnable { private ChatServer chatServer; private Socket connectionToClient; From efff828f1a7c17ecffe0a5a376bfaadf3d79ee9e Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:45:11 +0100 Subject: [PATCH 17/24] refactoring: indentation --- src/main/java/ClientHandler.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index bbc6455..db9059f 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -8,16 +8,11 @@ public class ClientHandler implements Runnable { private BufferedReader fromClientReader; private PrintWriter toClientWriter; - public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; name = connectionToClient.getInetAddress().getHostAddress(); - new Thread(this).start(); - } - - - + new Thread(this).start();} @Override public void run() { From c93929ef03d760247a5e8c18c01e492286937699 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:51:33 +0100 Subject: [PATCH 18/24] refactoring: identation run() --- src/main/java/ClientHandler.java | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index db9059f..7b96316 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -16,27 +16,32 @@ public class ClientHandler implements Runnable { @Override public void run() { + try { - fromClientReader = new BufferedReader(new InputStreamReader(connectionToClient.getInputStream())); - toClientWriter = new PrintWriter(new OutputStreamWriter(connectionToClient.getOutputStream())); + fromClientReader = new BufferedReader (new InputStreamReader(connectionToClient.getInputStream())); + toClientWriter = new PrintWriter (new OutputStreamWriter(connectionToClient.getOutputStream())); chatServer.broadcastMessage(name + " connected."); String message = fromClientReader.readLine(); while (message!=null) { chatServer.broadcastMessage(name + ": " + message); message = fromClientReader.readLine(); - } - } catch (IOException e) { - throw new RuntimeException(e); - - } finally { + } + } + + catch (IOException e) { + throw new RuntimeException(e); + } + + finally { //chatServer.removeClient(this); chatServer.broadcastMessage(name + " disconnected."); if (fromClientReader != null) { try { fromClientReader.close(); - } catch (IOException e){ + } + catch (IOException e){ e.printStackTrace(); } } @@ -50,5 +55,4 @@ public class ClientHandler implements Runnable { toClientWriter.println(message); toClientWriter.flush(); } - } From e334c8f1d09a0677c24d6390c3a06d07b0bd2e9f Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:59:03 +0100 Subject: [PATCH 19/24] refactoring: ClientHandler constructor comments --- src/main/java/ClientHandler.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 7b96316..9f60c96 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -8,12 +8,14 @@ public class ClientHandler implements Runnable { private BufferedReader fromClientReader; private PrintWriter toClientWriter; + // Constructor for ClientHandler public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; - name = connectionToClient.getInetAddress().getHostAddress(); - new Thread(this).start();} - + name = connectionToClient.getInetAddress().getHostAddress(); // Use the client's IP address as their name for simplicity + + new Thread(this).start();} // Start a new thread for this client handler + @Override public void run() { From b5e5dc248b96f006946cecc0734f84ecdad2e314 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 14:03:07 +0100 Subject: [PATCH 20/24] refactoring: comments run() try --- src/main/java/ClientHandler.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 9f60c96..b6ae287 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -20,12 +20,16 @@ public class ClientHandler implements Runnable { public void run() { try { + // Set up input and output streams for communication with the client fromClientReader = new BufferedReader (new InputStreamReader(connectionToClient.getInputStream())); toClientWriter = new PrintWriter (new OutputStreamWriter(connectionToClient.getOutputStream())); - chatServer.broadcastMessage(name + " connected."); + chatServer.broadcastMessage(name + " connected."); // Broadcast a message when the client is connected + + // Read messages from the client and broadcast them to all clients String message = fromClientReader.readLine(); while (message!=null) { + // Broadcast the client's message to all connected clients chatServer.broadcastMessage(name + ": " + message); message = fromClientReader.readLine(); } From cb912e039b3db0c41c518910fcce6df4e50eeb69 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 15:25:53 +0100 Subject: [PATCH 21/24] refactoring: comments in catch, finally in run() --- src/main/java/ClientHandler.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index b6ae287..5d943af 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -32,17 +32,17 @@ public class ClientHandler implements Runnable { // Broadcast the client's message to all connected clients chatServer.broadcastMessage(name + ": " + message); message = fromClientReader.readLine(); - } + } } catch (IOException e) { - throw new RuntimeException(e); + throw new RuntimeException(e); // Handle exceptions by throwing a runtime exception } - finally { //chatServer.removeClient(this); - chatServer.broadcastMessage(name + " disconnected."); - + chatServer.broadcastMessage(name + " disconnected."); // Broadcast a message when the client is disconnected + + // Close resources in the finally block if (fromClientReader != null) { try { fromClientReader.close(); @@ -54,7 +54,7 @@ public class ClientHandler implements Runnable { if (toClientWriter != null) { toClientWriter.close(); } - } + } } public void sendMessage(String message) { From 33b7f9e3b9dd99fd3fab3a0f768d55c6c4745475 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 15:28:11 +0100 Subject: [PATCH 22/24] refactoring: comments and identation on sendMessage Method --- src/main/java/ClientHandler.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 5d943af..fa4b924 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -54,11 +54,12 @@ public class ClientHandler implements Runnable { if (toClientWriter != null) { toClientWriter.close(); } - } + } } - - public void sendMessage(String message) { - toClientWriter.println(message); - toClientWriter.flush(); + + //Method to send a message to the client + public void sendMessage(String message) { + toClientWriter.println(message); // Send the message to the client + toClientWriter.flush(); // Flush the stream } } From 6f751f7eab52ab6313296d622f82bbaeaa05dd10 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 15:44:08 +0100 Subject: [PATCH 23/24] deleted:test --- src/main/java/test | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/test diff --git a/src/main/java/test b/src/main/java/test deleted file mode 100644 index e69de29..0000000 From 3f4a302efcc17102265600cf5369c20af4fed0a5 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Mon, 5 Feb 2024 21:19:44 +0100 Subject: [PATCH 24/24] Establish connection to server --- src/main/java/ChatClient.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 695f622..8d0b776 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -42,9 +42,7 @@ public class ChatClient extends JFrame implements KeyListener { } private void receiveMessages() { try { - // Um GUI zu testen - // Kommentar in der Zeile 28 wird weggenohmen, wenn der Server erstellt wird. - // connectionToServer = new Socket(address, port); + connectionToServer = new Socket(address, port); initGui(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen.");