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. 1
      .classpath
  2. 11
      src/main/java/ChatServer.java
  3. 4
      src/main/java/ClientHandler.java

1
.classpath

@ -36,5 +36,6 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

11
src/main/java/ChatServer.java

@ -39,10 +39,10 @@ public class ChatServer {
} }
// Methode, um eine Nachricht an alle verbundenen Clients zu senden // Methode, um eine Nachricht an alle verbundenen Clients zu senden
public void broadcastMessage(String message) { 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) { public void removeClient(ClientHandler client) {
clients.remove(client); clients.remove(client);
} }
public List<ClientHandler> getClients() {
return clients;
}
} }

4
src/main/java/ClientHandler.java

@ -1,6 +1,8 @@
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import javax.swing.JOptionPane;
public class ClientHandler implements Runnable { public class ClientHandler implements Runnable {
private ChatServer chatServer; private ChatServer chatServer;
private Socket connectionToClient; private Socket connectionToClient;
@ -12,7 +14,7 @@ public class ClientHandler implements Runnable {
public ClientHandler(ChatServer chatServer, Socket connectionToClient) { public ClientHandler(ChatServer chatServer, Socket connectionToClient) {
this.chatServer = chatServer; this.chatServer = chatServer;
this.connectionToClient = connectionToClient; 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 new Thread(this).start();} // Start a new thread for this client handler

Loading…
Cancel
Save