CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Location
    Caracas, Venezuela
    Posts
    32

    Question 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!
    Last edited by doy2001; November 8th, 2004 at 02:34 AM.

  2. #2
    Join Date
    Sep 2004
    Posts
    83

    Re: Weird server program behaviour...

    it's working fine.

    Code:
    String line = in.readLine();
    is blocking for input.

  3. #3
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    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...)
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  4. #4
    Join Date
    Mar 2002
    Location
    Holland
    Posts
    279

    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.
    Code:
     (!done)
    the ! is a logical not operation.
    meaning true is false and false is true.
    so the code is
    Code:
     (!done) = true
    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
    A VB programmer trying to stay alive in a Real C World

    If the hardware is so great.. why use software to correct it..?? It will only slow it down..
    Al is de hardware nog zo snel de software achterhaalt het wel

  5. #5
    Join Date
    Jul 2004
    Location
    Caracas, Venezuela
    Posts
    32

    Post 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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured