Results 1 to 2 of 2
  1. #1
    vaas45 is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Problem with Bufferedreader

    hey!
    I am writing a simple gui chat application.The following code when run is producing an error i.e in.ready is always being false even when i send data to the server.tell me if there is any error.
    server:
    Java Code:
      try {
                ss = new ServerSocket(9999);
            } catch (IOException ex) {
                Logger.getLogger(connection.class.getName()).log(Level.SEVERE, null, ex);
            }
    
            try {
                clientsocket = ss.accept();
                System.out.println(ss);
                System.out.println(clientsocket);
    
            } catch (IOException ex) {
                Logger.getLogger(connection.class.getName()).log(Level.SEVERE, null, ex);
            }
            out = new PrintWriter(clientsocket.getOutputStream(), true);
            in = new BufferedReader(
    				new InputStreamReader(
    				clientsocket.getInputStream()));
            while(!in.ready())
            {    System.out.println(clientsocket);
                System.out.println("not ready");
            }
          in.readLine();

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    6

    Default

    What you're doing is called polling. Polling is generally bad. It eats your CPU.

    First of all, why do you care if the reader is ready? Why not just call readLine?

    If you want to see that your program is working, consider this design pattern:

    Java Code:
    clientSocket.setSoTimeout(1000);
    String line = null;
    while(line == null) {
       try {
          line = in.readLine();
       }
       catch (SocketTimeoutException e) {
          System.err.println("Still waiting...");
          continue;
       }
    }
    // do something with your line
    This will print "Still waiting..." approximately once per second until something is read from the socket.

    Normally, you'd put the "do something" part inside the loop, and perform some other test to see if you should break out of the loop.
    Last edited by kjkrum; 04-25-2011 at 04:36 AM.

Similar Threads

  1. BufferedReader problem
    By dilpreet28 in forum New To Java
    Replies: 5
    Last Post: 07-05-2010, 12:18 PM
  2. BufferedReader
    By Nerijus in forum New To Java
    Replies: 4
    Last Post: 04-26-2010, 07:28 PM
  3. BufferedReader, need help!
    By zacharyrod in forum New To Java
    Replies: 10
    Last Post: 11-19-2009, 10:56 AM
  4. problem with BufferedReader
    By Ozmosis in forum Networking
    Replies: 6
    Last Post: 10-29-2009, 09:07 PM
  5. Replies: 1
    Last Post: 01-15-2008, 08:55 PM

Posting Permissions

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