BufferReader using reaLine() is not terminating when end of message.

Hi folks,

Newby Java here. I have a situation where I am writing a Server Socket Java program where I am accepting connection from a client and the client is sending messages me. I am able to connected to the client and the client successfully send messages to me, but the problem I am running into is that it's not terminating the while loop when I read to the end of the message using a reaLine(). It's like it's hung up or something...

Here is a copy of the code.....

ss = new ServerSocket(6500, 5);

System.out.println("Listenning........waiting for connection from client");

try {

response = ss.accept();

System.out.println("Accepted connection from client");

InputStream in = response.getInputStream();

System.out.println("Input Stream Opened");

BufferedReader br = new BufferedReader(new InputStreamReader(in));

System.out.println("Buffered Stream Opened");

System.out.println("Server received: " + "\n" );

BufferedWriter bw = new BufferedWriter(new FileWriter("testing001.txt"));

String input = br.readLine();

while (input != null) {

bw.write(input);

bw.newLine();

input = br.readLine();

}

br.close();

bw.close();

System.out.println("Done receiving message" + "\n" );

OutputStream out = response.getOutputStream();

BufferedWriter buf = new BufferedWriter(new OutputStreamWriter(out));

buf.write(result);

System.out.println("Server sent: "+result);

buf.flush();

buf.close();

response.close();

ss.close();

}

catch (IOException e) {

System.out.println("Failure accepting connection: " + e);

}

What am I doing wrong that the while loop does not terminate when it reads to end of the message using this "readLine()"?

It does not execute the next statement after the while-loop unless you disconnect the connection from the client.

Help!

[1971 byte] By [newbyJavaa] at [2007-10-2 6:36:17]
# 1

readLine() will wait for more lines forever, or until the other end closes the connection. (For completeness: there are other termination situations too, mostly error conditions.)

You have a multi-line message? Send some kind of an end-of-message line and check for it in your read loop. E.g. "." on a line all by itself. Caveat: if the message itself can contain a line like that you'll need to quote that line somehow. Another approach: send the number of lines in the message first. Requires that you know the number beforehand.

sjasjaa at 2007-7-16 13:38:49 > top of Java-index,Java Essentials,Java Programming...
# 2
The client will send messages variable without me knowing how many lines. It can be any amount of lines.What other read command can I use then besides the readLine() so that it'll terminate?
newbyJavaa at 2007-7-16 13:38:49 > top of Java-index,Java Essentials,Java Programming...
# 3
Do you think I can have the client put End of Block after each message and can the readLine() detect that or any other read command?
newbyJavaa at 2007-7-16 13:38:49 > top of Java-index,Java Essentials,Java Programming...
# 4

Yes. That's what sjasja said: "Send some kind of an end-of-message line and check for it in your read loop." Now, I don't know what "End of Block" is, but if it's some special text followed by an end-of-line character then that would work fine. Remember that your server only understands text by design.

DrClapa at 2007-7-16 13:38:49 > top of Java-index,Java Essentials,Java Programming...