Hey daniweb!
I have a really serious issue in my program right now.
I'll explain how this piece works:
-The server sends a ping
-The client replies with a pong and starts a timer
-Server sends pong back and the client checks how long it took to receive the second pong (Latency check)
Right now the client is spaming pong to the server without delay. I think this is the case because when i shut down the client the server acts as normal without mass errors.
The clients code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import javax.swing.Timer;
public class dataIn implements Runnable, ActionListener {
static BufferedReader in;
static String ex1;
Boolean threadcontinue = true;
static String o;
static String o1;
Socket s;
Timer t1 = new Timer(1, this);
public static int PingTime = 0;
dataIn(Socket s) {
this.s = s;
}
public void run() {
// TODO Auto-generated method stub
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println("dataIn started.");
while(threadcontinue) {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
ex1 = in.readLine();
if(ex1.equalsIgnoreCase("ping")==true) {
System.out.println("received ping");
PrintStream pstream2 = new PrintStream(s.getOutputStream());
pstream2.println("pong");
t1.start();
System.out.println("pong sent!!");
} else if(ex1.equalsIgnoreCase("pong")) {
System.out.println("received pong");
System.out.println("Latency: " + PingTime + "ms.");
ClientApplet.io1 = PingTime + " ms.";
PingTime = 0;
t1.stop();
}
}
} catch (Throwable t) {
// TODO Auto-generated catch block
//t.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e2) {
// TODO Auto-generated method stub
if (e2.getSource() == t1) {
PingTime = PingTime + 1;
if(PingTime > 10000) {
System.out.println("t1 stopped");
t1.stop();
}
}
}
}
What's the issue with it sending pong so much?
ex1 is the variable holding the socket data. I tried starting from there but couldn't find anything :s
Thank you so so much in advance