Weird server program behaviour...
Hi! I have a question regarding a simple server program included as a sample on my Core Java book. Here´s the code:
Code:
/**
* @version 1.10 1997-06-27
* @author Cay Horstmann
*/
import java.io.*;
import java.net.*;
/**
This program implements a simple server that listens to
port 8189 and echoes back all client input.
*/
public class EchoServer
{
public static void main(String[] args )
{
try
{
// establish server socket
ServerSocket s = new ServerSocket(8189);
// wait for client connection
Socket incoming = s.accept( );
BufferedReader in = new BufferedReader
(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter
(incoming.getOutputStream(), true /* autoFlush */);
out.println( "Hello! Enter BYE to exit.");
// echo client input
boolean done = false;
while (!done)
{
String line = in.readLine();
if (line == null) done = true;
else
{
out.println("Echo: " + line);
if (line.trim().equals("BYE"))
done = true;
}
}
incoming.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Ok, try running the program yourselves and then open a local connection on port 8189. My question is: it seems the "while" is not running while done == false... it seems to run only when the server gets input. Why is this? it should run always, and read a line from the BufferedReader always. If the buffer has nothing, then there is no line, and done = true. So practically the program should end up a second after starting.
Thanks for your help... this is driving me nuts!
Re: Weird server program behaviour...
it's working fine.
Code:
String line = in.readLine();
is blocking for input.
Re: Weird server program behaviour...
The only way the server will terminate is if the client entered "BYE" or sent EOF. BufferedReader's readLine() method works by waiting until it receives something, it will wait forever if it has too (you can test this out yourself but I don't think you have forever to wait...)
Re: Weird server program behaviour...
Quote:
Originally Posted by doy2001
Hi! I have a question regarding a simple server program included as a sample on my Core Java book. Here´s the code:
[CODE
// echo client input
boolean done = false;
while (!done)
{
String line = in.readLine();
if (line == null) done = true;
else
{
out.println("Echo: " + line);
if (line.trim().equals("BYE"))
done = true;
}
}
[/CODE]
ok.. lets do it in english...
Set done to false
Here comes the tricky part. so I'll split it.
the ! is a logical not operation.
meaning true is false and false is true.
so the code is So we are entering the while loop
when done is set to true this means that in the compair the result is false.
and this will end the program.
I hope this explains it a little bit more.
if not, reply to this post and I'll try again.
with kind regards,
Jewe
Re: Weird server program behaviour...
Thanks for the replies. Jewe, that wasn´t what i was looking for, but thanks anyway. cma and jw_wvu you´ve got my point. I didn´t know that in.readLine blocks until it recieves some input. I thought that if the buffer was empty then in.readLine would still continue reading empty lines... I was wrong. Thanks!