Results 1 to 2 of 2
Thread: Problem with Bufferedreader
- 04-21-2011, 11:03 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
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();
- 04-25-2011, 04:33 AM #2
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:
This will print "Still waiting..." approximately once per second until something is read from the socket.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
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
-
BufferedReader problem
By dilpreet28 in forum New To JavaReplies: 5Last Post: 07-05-2010, 12:18 PM -
BufferedReader
By Nerijus in forum New To JavaReplies: 4Last Post: 04-26-2010, 07:28 PM -
BufferedReader, need help!
By zacharyrod in forum New To JavaReplies: 10Last Post: 11-19-2009, 10:56 AM -
problem with BufferedReader
By Ozmosis in forum NetworkingReplies: 6Last Post: 10-29-2009, 09:07 PM -
Problem either with BufferedReader or sending a newline character
By aikanaro in forum NetworkingReplies: 1Last Post: 01-15-2008, 08:55 PM


LinkBack URL
About LinkBacks

Bookmarks