My ServerSocket writes the following lines:
OutputStreamWriter outstream = new OutputStreamWriter(clientSocket.getOutputStream()); BufferedWriter out = new BufferedWriter(outstream); out.write("Hello"); out.newLine(); out.write("People"); out.flush();
And my client reads it like this:
in = new BufferedReader(new InputStreamReader(connection.getInputStream())); while(true){ line = in.readLine(); if(line == null){ ClientDialog.gui.log.append("NULL LINE\r\n"); } else{ ClientDialog.gui.log.append(line+"\r\n"); } if(in.readLine() == "SHUTDOWN"){ break; } }
As you can see, I write โHelloโ, a new line, and then โPeopleโ on the socket, but when I start my client, it only prints โHelloโ and null again. I do not see what is wrong?
PROBLEM SOLVED:
I had to add out.newLine() after I wrote "People" to the socket, and I had to do line == "SHUTDOWN" not in.readLine() == "SHUTDOWN" when in.readLine() consumed " People. "
It is also recommended that you use the equals() method in the String class instead of == .
Thanks!
This is for future viewers.
user569322
source share