From 50542246539506261da8091dbddc2957fb21a9f9 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Sun, 10 Apr 2022 10:56:59 +0200 Subject: [PATCH 1/3] Make ServerConnectionHandler runnable => fixed multiple client connection to server --- .../src/main/java/ch/zhaw/pm2/multichat/server/Server.java | 2 +- .../zhaw/pm2/multichat/server/ServerConnectionHandler.java | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java b/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java index 1f903ce..1edb279 100644 --- a/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java +++ b/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java @@ -67,7 +67,7 @@ public class Server { while (true) { NetworkHandler.NetworkConnection connection = networkServer.waitForConnection(); ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections); - connectionHandler.startReceiving(); + new Thread(connectionHandler).start(); System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>", connectionHandler.getUserName(), connection.getRemoteHost(), diff --git a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java index 4c37948..5d9a0f7 100644 --- a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java +++ b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java @@ -14,7 +14,7 @@ import java.util.concurrent.atomic.AtomicInteger; import static ch.zhaw.pm2.multichat.server.ServerConnectionHandler.State.*; -public class ServerConnectionHandler { +public class ServerConnectionHandler implements Runnable{ private static final AtomicInteger connectionCounter = new AtomicInteger(0); private final int connectionId = connectionCounter.incrementAndGet(); private final NetworkHandler.NetworkConnection connection; @@ -33,6 +33,11 @@ public class ServerConnectionHandler { private String userName = "Anonymous-"+connectionId; private State state = NEW; + @Override + public void run() { + startReceiving(); + } + enum State { NEW, CONNECTED, DISCONNECTED; } From 361037ab9a95e70d37e069dd795566d2ec1e0a98 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Sun, 10 Apr 2022 14:14:11 +0200 Subject: [PATCH 2/3] Make NetworkConnection send(T data) method synchronized --- .../java/ch/zhaw/pm2/multichat/protocol/NetworkHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/NetworkHandler.java b/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/NetworkHandler.java index 15cc22b..6c58849 100644 --- a/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/NetworkHandler.java +++ b/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/NetworkHandler.java @@ -305,7 +305,7 @@ public class NetworkHandler { * @param data data object of type T to be submitted through the connection. * @throws IOException if an error occurs (e.g. connection interrupted while sending, ...) */ - public void send(T data) throws IOException { + public synchronized void send(T data) throws IOException { ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream()); outputStream.writeObject(data); } From 60338e4bbb5d438cf4e15857d1a4fa6516bd148e Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Sun, 10 Apr 2022 16:48:15 +0200 Subject: [PATCH 3/3] GUI: Clear messagefield after text has been processed --- .../java/ch/zhaw/pm2/multichat/client/ChatWindowController.java | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java index b48a249..e9a704e 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java @@ -86,6 +86,7 @@ public class ChatWindowController { return; } String messageString = messageField.getText().strip(); + messageField.clear(); Matcher matcher = messagePattern.matcher(messageString); if (matcher.find()) { String receiver = matcher.group(1);