diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/Multiplayer/helpers/ClientPinger.java b/src/main/java/ch/unibas/dmi/dbis/cs108/Multiplayer/helpers/ClientPinger.java index 86822f4..669fbd3 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/Multiplayer/helpers/ClientPinger.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/Multiplayer/helpers/ClientPinger.java @@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers; import java.io.BufferedWriter; import java.io.IOException; +import java.net.Socket; /** * Sends a ping to the server ("CPING") every 2 seconds and checks if it has gotten a pingback. @@ -12,27 +13,28 @@ public class ClientPinger implements Runnable{ private boolean gotPingBack; //should be set to true (via setGotPingBack) as soon as the client gets a pingback. private boolean isConnected; //set to true unless the ClientPinger detects a connection loss. BufferedWriter out; //the output of this client through which the pings are sent - public boolean isRunning; //can be set to false to tell the ping-er to quit + private Socket socket; /** - * + * @param socket the socket the Client is connected to which is used to end the thread if the connection is lost. * @param out the output through which the pings are sent. */ - public ClientPinger(BufferedWriter out) { + public ClientPinger(BufferedWriter out, Socket socket) { gotPingBack = false; isConnected = true; this.out = out; - isRunning = true; + this.socket = socket; } @Override public void run() { try { - while (isRunning) { + while (socket.isConnected()) { out.write("CPING"); Thread.sleep(2000); if (!gotPingBack) isConnected = false; } + isConnected = false; //in case the socket accidentally disconnects (can this happen?) } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException e) { diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/Multiplayer/helpers/ServerPinger.java b/src/main/java/ch/unibas/dmi/dbis/cs108/Multiplayer/helpers/ServerPinger.java index 2b30bec..ac9cebc 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/Multiplayer/helpers/ServerPinger.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/Multiplayer/helpers/ServerPinger.java @@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers; import java.io.BufferedWriter; import java.io.IOException; +import java.net.Socket; /** * Sends a ping to the client ("SPING") every 2 seconds and checks if it has gotten a pingback. @@ -12,27 +13,28 @@ public class ServerPinger implements Runnable{ private boolean gotPingBack; //should be set to true (via setGotPingBack) as soon as the server gets a pingback. private boolean isConnected; //set to true unless the ServerPinger detects a connection loss. BufferedWriter out; //the output of this client through which the pings are sent - public boolean isRunning; //can be set to false to tell the ping-er to quit + private Socket socket; /** - * + * @param socket the socket the ClientHandler is connected to; used to end the thread if the connection is lost. * @param out the output through which the pings are sent. */ - public ServerPinger(BufferedWriter out) { + public ServerPinger(BufferedWriter out, Socket socket) { gotPingBack = false; isConnected = true; this.out = out; - isRunning = true; + this.socket = socket; } @Override public void run() { try { - while (isRunning) { + while (socket.isConnected()) { out.write("SPING"); Thread.sleep(2000); if (!gotPingBack) isConnected = false; } + isConnected = false; //in case the socket accidentally disconnects (can this happen?) } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException e) {