Clean up.

This commit is contained in:
Sebastian Lenzlinger 2022-03-24 15:49:05 +01:00
parent 65404c210b
commit 3c89b60592
5 changed files with 23 additions and 9 deletions

View File

@ -16,7 +16,11 @@ public class Client {
this.socket = socket;
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.in = new BufferedReader((new InputStreamReader((socket.getInputStream()))));
this.userName = userName;
this.out.write(getUsername());
this.out.newLine();
this.out.flush();
} catch (IOException e) {
e.printStackTrace();
closeEverything(socket, in, out);

View File

@ -11,7 +11,7 @@ public class ClientHandler implements Runnable {
private BufferedReader in;
private Socket socket;
Scanner sc;
public static HashSet<ClientHandler> clientHandlers = new HashSet<>();
public static HashSet<ClientHandler> connectedClients = new HashSet<>();
public static HashSet<ClientHandler> inGameClients = new HashSet<>();
public static HashSet<ClientHandler> ghostClients = new HashSet<>();
@ -21,7 +21,7 @@ public class ClientHandler implements Runnable {
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.in = new BufferedReader((new InputStreamReader((socket.getInputStream()))));
this.clientUserName = in.readLine();
clientHandlers.add(this);
connectedClients.add(this);
broadcastMessage("SERVER: " + clientUserName + " has joined the Server");
} catch (IOException e) {
e.printStackTrace();
@ -55,17 +55,15 @@ public class ClientHandler implements Runnable {
}
public void broadcastMessage(String msg) {
for (ClientHandler client : clientHandlers) {
for (ClientHandler client : connectedClients) {
try {
if(!client.clientUserName.equals((clientUserName))) {
client.out.write(msg);
client.out.newLine();
client.out.flush();
} else {
client.out.write("Message: **" + msg + "** sent!");
client.out.newLine();
client.out.flush();
}
client.out.newLine();
client.out.flush();
} catch (IOException e) {
e.printStackTrace();
closeEverything(socket, in ,out);
@ -74,7 +72,7 @@ public class ClientHandler implements Runnable {
}
public void removeClientHandler() {
clientHandlers.remove(this);
connectedClients.remove(this);
broadcastMessage("SERVER: " + clientUserName + " has left the server");
}

View File

@ -19,7 +19,7 @@ public class Server {
public void startServer() {
try {
System.out.println("Port 42069 open on ");
System.out.println("Port 42069 is open on " + this.serverSocket.getInetAddress());
while (!serverSocket.isClosed()) {
Socket socket = serverSocket.accept();
ClientHandler nextClient = new ClientHandler(socket);

View File

@ -0,0 +1,8 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Server;
/**
* Implements the communication protocol in the connecting phase.
* After this one can consider the server and client in session;
*/
public class connectingLogik {
}

View File

@ -0,0 +1,4 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Server;
public class sessionLogik {
}