I am making a java applet that has basic chat functionality (you can send/recieve messages). I have made a separate thread to handle the connection on the client version, and the server also makes a thread for every connected client.
in the run() method of the client i have a while loop to read any messages recieved:
while (state == ConnectionState.CONNECTED) {
out.println("Hello Server");
out.flush();
String input = in.readLine();
System.out.println(input);
if(input == null){
connectionClosedFromOtherSide();
}
else {
received(input);
System.out.println(String.format("Recieved something: %s", input));
}
}
Here out is my PrintWriter and in my BufferedReader. For debugging purposes I put the ‘hello server’ here. This code works fine, but the problem is the sendtext() method:
synchronized private void sendText(String tosend) {
if(out != null && state == ConnectionState.CONNECTED){
out.print(tosend);
out.flush();
System.out.println(String.format("sending %s to server", tosend));
}
}
For some reason, if i call this method, it isn’t recieved on the server side. (however the console does show that it is sent)
Can anyone please help me out with this?
Client side, each
should be, on server side, balanced by
out.flush()don’t send a newline.