My ServerSocket writes out 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 so:
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 run my client, it only prints “Hello” and null repeatedly. I don’t see what is wrong?
PROBLEM SOLVED:
I had to add an out.newLine() after I wrote “People” to the socket, and I had to do line == "SHUTDOWN" not in.readLine() == "SHUTDOWN" as the in.readLine() was consuming “People”.
It was also recommended to use the equals() method in the String class, instead of ==.
Thanks!
This is for future viewers.
You call readLine twice per loop.
The code above consumes your “People” line
to fix change to
Also you should send a newline after sending “People”