Browse Source

Merge branch 'server' into 'main'

Server

See merge request fdai7332/java-chat!7
remotes/origin/server
fdai7599 11 months ago
parent
commit
865898a0b0
  1. 81
      .classpath
  2. 11
      src/main/java/ChatServer.java
  3. 6
      src/main/java/ClientHandler.java

81
.classpath

@ -1,40 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

11
src/main/java/ChatServer.java

@ -39,10 +39,10 @@ public class ChatServer {
}
// Methode, um eine Nachricht an alle verbundenen Clients zu senden
public void broadcastMessage(String message) {
System.out.println(message);
if (message != null) {
for (ClientHandler client : clients) {
client.sendMessage(message); // Nachricht an jeden Client senden
System.out.println(message);
if (message != null) {
for (ClientHandler client : clients) {
client.sendMessage(message);
}
}
@ -56,5 +56,8 @@ public class ChatServer {
public void removeClient(ClientHandler client) {
clients.remove(client);
}
public List<ClientHandler> getClients() {
return clients;
}
}

6
src/main/java/ClientHandler.java

@ -1,6 +1,8 @@
import java.io.*;
import java.net.Socket;
import javax.swing.JOptionPane;
public class ClientHandler implements Runnable {
private ChatServer chatServer;
private Socket connectionToClient;
@ -12,7 +14,7 @@ public class ClientHandler implements Runnable {
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
name = JOptionPane.showInputDialog("Benutzername für neuen Client vergeben");
new Thread(this).start();} // Start a new thread for this client handler
@ -62,4 +64,4 @@ public class ClientHandler implements Runnable {
toClientWriter.println(message); // Send the message to the client
toClientWriter.flush(); // Flush the stream
}
}
}
Loading…
Cancel
Save