student
student

Reputation: 23

reading from input stream of socket without busy waiting in JAVA

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

Upvotes: 1

Views: 2070

Answers (2)

trashgod
trashgod

Reputation: 205775

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.

Upvotes: 1

Fried Hoeben
Fried Hoeben

Reputation: 3272

Have you looked asynchronous IO?

Upvotes: 2

Related Questions