Updated ServerPinger and ClientPinger to terminate if the socket is closed

This commit is contained in:
Jonas 2022-03-24 20:22:40 +01:00
parent 04df8806cd
commit 47703e481e
2 changed files with 14 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; 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. * 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 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. 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 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. * @param out the output through which the pings are sent.
*/ */
public ClientPinger(BufferedWriter out) { public ClientPinger(BufferedWriter out, Socket socket) {
gotPingBack = false; gotPingBack = false;
isConnected = true; isConnected = true;
this.out = out; this.out = out;
isRunning = true; this.socket = socket;
} }
@Override @Override
public void run() { public void run() {
try { try {
while (isRunning) { while (socket.isConnected()) {
out.write("CPING"); out.write("CPING");
Thread.sleep(2000); Thread.sleep(2000);
if (!gotPingBack) isConnected = false; if (!gotPingBack) isConnected = false;
} }
isConnected = false; //in case the socket accidentally disconnects (can this happen?)
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {

View File

@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; 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. * 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 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. 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 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. * @param out the output through which the pings are sent.
*/ */
public ServerPinger(BufferedWriter out) { public ServerPinger(BufferedWriter out, Socket socket) {
gotPingBack = false; gotPingBack = false;
isConnected = true; isConnected = true;
this.out = out; this.out = out;
isRunning = true; this.socket = socket;
} }
@Override @Override
public void run() { public void run() {
try { try {
while (isRunning) { while (socket.isConnected()) {
out.write("SPING"); out.write("SPING");
Thread.sleep(2000); Thread.sleep(2000);
if (!gotPingBack) isConnected = false; if (!gotPingBack) isConnected = false;
} }
isConnected = false; //in case the socket accidentally disconnects (can this happen?)
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {