tags:

views:

271

answers:

2

i want to read from input stream when i connect to a server socket. but there may exist some delays between messages how can i read from input stream without busy loop ?

string mes = socketReader.readLine();

above line returns null when no input provided in socket input stream. i want to be notified somehow when a message is ready in input stream.

tnx

+1  A: 

In a GUI context, SwingWorker may help: let doInBackground() do the reading, use process() for interim reuslts, and done() to wrap up. You can register a PropertyChangeListener to drive a progress indicator. This article expands on the idea, and a back port to Java 1.5 is available. Here's a simple example that reads from a JDBC source instead of a stream, but the idea is the same.

trashgod
+2  A: 

Have you looked asynchronous IO?

Fried Hoeben
NIO is the prettiest way to go about it, but the API is somewhat intricate. Consider one of the NIO wrappers such as Grizzly, NeTTY, NIOFramework or Apache MINA.
Barend