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.socket = socket;
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.in = new BufferedReader((new InputStreamReader((socket.getInputStream())))); this.in = new BufferedReader((new InputStreamReader((socket.getInputStream()))));
this.userName = userName; this.userName = userName;
this.out.write(getUsername());
this.out.newLine();
this.out.flush();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
closeEverything(socket, in, out); closeEverything(socket, in, out);

View File

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

View File

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