CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  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.

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