so i am working on a project where I am making an SMTP server. I am trying to read in each line of input and then hand that line to another thread for processing. However, with this code, the lines aren't sent until the connection is ended. I think it has to do with the fact that a new line doesn't break the while loop:
while (true) {
StringBuilder line = new StringBuilder();
try {
int c;
while ((c = in.read()) != -1){ //this reads the line
line.append((char) c); //add the character to our line
}
String response = Parser.parse(line.toString); //give the line we just read to the parser for processing
} catch (IOException ex) {
System.err.println("IOException while reading the clients mesasge");
}
}
I am also going to quote "Java Network Programming 4th edition" by Elliot Harold because the author says that we shouldn't use the readLine() method of a BufferedReader or a DataInputStream because different operating systems have different newline delimeters (\n or \r\n or something else)
Also, I'm not using either of those streams so even if i wanted to use the readLine() I wouldn't be able to do.
Anyone have any idea how I can accomplish what I'm trying to do
while ((c = in.read()) != -1){ //this reads the line
No it doesn't. It reads one character.
Use BufferedReader.readLine()
.
I am also going to quote "Java Network Programming 4th edition" by Elliot Harold because the author says that we shouldn't use the readLine() method of a BufferedReader or a DataInputStream because different operating systems have different newline delimeters (\n or \r\n or something else).
Elliot Rusty Harold needs to read the Javadoc, and so do you. This is grade A BS. See the reviews at Amazon for a long list of mistakes in this book.