Browse Source
Merge branch 'main' of https://gitlab.cs.hs-fulda.de/fdai7332/java-chat into richardBranch
remotes/origin/feature/client/make-input-field-buttons-interactive
Merge branch 'main' of https://gitlab.cs.hs-fulda.de/fdai7332/java-chat into richardBranch
remotes/origin/feature/client/make-input-field-buttons-interactive
Richard Schmidt
11 months ago
13 changed files with 295 additions and 5 deletions
-
23bin/.project
-
92bin/README.md
-
24bin/build-project.sh
-
4bin/target/classes/META-INF/MANIFEST.MF
-
7bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties
-
6bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml
-
0bin/target/classes/test2
-
0bin/target/test-classes/test.txt
-
0bin/target/test-classes/text.txt
-
5bin/team.md
-
70src/main/java/ChatClient.java
-
65src/main/java/ClientHandler.java
@ -0,0 +1,23 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<projectDescription> |
||||
|
<name>java-chat</name> |
||||
|
<comment></comment> |
||||
|
<projects> |
||||
|
</projects> |
||||
|
<buildSpec> |
||||
|
<buildCommand> |
||||
|
<name>org.eclipse.jdt.core.javabuilder</name> |
||||
|
<arguments> |
||||
|
</arguments> |
||||
|
</buildCommand> |
||||
|
<buildCommand> |
||||
|
<name>org.eclipse.m2e.core.maven2Builder</name> |
||||
|
<arguments> |
||||
|
</arguments> |
||||
|
</buildCommand> |
||||
|
</buildSpec> |
||||
|
<natures> |
||||
|
<nature>org.eclipse.jdt.core.javanature</nature> |
||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature> |
||||
|
</natures> |
||||
|
</projectDescription> |
@ -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. |
@ -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 |
@ -0,0 +1,4 @@ |
|||||
|
Manifest-Version: 1.0 |
||||
|
Build-Jdk-Spec: 17 |
||||
|
Created-By: Maven Integration for Eclipse |
||||
|
|
@ -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 |
@ -0,0 +1,6 @@ |
|||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
<groupId>org.progmethoden</groupId> |
||||
|
<artifactId>java-chat</artifactId> |
||||
|
<version>0.0.1-SNAPSHOT</version> |
||||
|
</project> |
@ -0,0 +1,5 @@ |
|||||
|
- Valentin Spiroski, fdai7332 |
||||
|
- Richard Schmidt, fdai7420 |
||||
|
- Paul Kattenborn, fdai7599 |
||||
|
- Marc Dimmerling, fdai7579 |
||||
|
- Alena Bandarovich, fdai5595 |
@ -1,12 +1,76 @@ |
|||||
import javax.swing.*; |
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 { |
|
||||
|
public class ChatClient extends JFrame implements KeyListener { |
||||
private String address; |
private String address; |
||||
public ChatClient() { |
|
||||
|
private int port; |
||||
|
private Socket connectionToServer; |
||||
|
|
||||
|
// GUI |
||||
|
private JTextArea outputTextArea; |
||||
|
private JTextField inputTextField; |
||||
|
|
||||
|
public ChatClient(int port) { |
||||
|
super("Chat"); |
||||
|
this.port = port; |
||||
address = JOptionPane.showInputDialog("bitte IP-Adresse"); |
address = JOptionPane.showInputDialog("bitte IP-Adresse"); |
||||
|
|
||||
|
if (address != null) { |
||||
|
receiveMessages(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void initGui() { |
||||
|
outputTextArea = new JTextArea(); |
||||
|
outputTextArea.setEditable(false); |
||||
|
outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); |
||||
|
|
||||
|
inputTextField = new JTextField(); |
||||
|
inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); |
||||
|
inputTextField.addKeyListener(this); |
||||
|
|
||||
|
add(outputTextArea, BorderLayout.CENTER); |
||||
|
add(inputTextField, BorderLayout.SOUTH); |
||||
|
|
||||
|
setVisible(true); |
||||
|
setSize(800, 600); |
||||
|
setDefaultCloseOperation(EXIT_ON_CLOSE); |
||||
|
} |
||||
|
private void receiveMessages() { |
||||
|
try { |
||||
|
connectionToServer = new Socket(address, port); |
||||
|
initGui(); |
||||
|
} catch (Exception e) { |
||||
|
JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@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) { |
public static void main(String[] args) { |
||||
new ChatClient(); |
|
||||
|
new ChatClient(3141); |
||||
} |
} |
||||
} |
} |
@ -0,0 +1,65 @@ |
|||||
|
import java.io.*; |
||||
|
import java.net.Socket; |
||||
|
|
||||
|
public class ClientHandler implements Runnable { |
||||
|
private ChatServer chatServer; |
||||
|
private Socket connectionToClient; |
||||
|
private String name; |
||||
|
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(); // 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() { |
||||
|
|
||||
|
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."); // 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(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
catch (IOException e) { |
||||
|
throw new RuntimeException(e); // Handle exceptions by throwing a runtime exception |
||||
|
} |
||||
|
finally { |
||||
|
//chatServer.removeClient(this); |
||||
|
chatServer.broadcastMessage(name + " disconnected."); // Broadcast a message when the client is disconnected |
||||
|
|
||||
|
// Close resources in the finally block |
||||
|
if (fromClientReader != null) { |
||||
|
try { |
||||
|
fromClientReader.close(); |
||||
|
} |
||||
|
catch (IOException e){ |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
if (toClientWriter != null) { |
||||
|
toClientWriter.close(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//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 |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue