This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Incoming Stream Issue  RSS feed

 
James Koehler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am reading the incoming stream, with say Incoming.read(). How do I exit out of this so I can run other code if nothing is currently being sent? Right now it gets stuck there until I send something to the stream.

James
 
James Koehler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well after trying for a few days I posted on here and soon after I figured out how to get around it. I found the available command did the trick.
If (Incoming.available() != 0) then Incoming.read();

James
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using available, probably you can look into this faq entry.
Available will give you whether data is available to read at that moment of time. So, probably, you would have to run in a loop with a sleep in between to ensure that you read the data when available. This sounds funny and so is the reason why read is blocking!
However, if you want a non-blocking version of I/O you can have a look on java nio.
[ September 17, 2007: Message edited by: Nitesh Kant ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can move your blocking reading logic onto its own thread ...

Does that look at all interesting?

There are many options for how to get data back from the reader before or after it finishes. Let us know more requirements if that's getting close.
 
James Koehler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thank you for the replies. I am new to Java and I do not have as much time as I'd like to program. I have not tried threads before and I tried what you had posted and it did not have the outcome I had hoped for although I probably didn't do it right.
So far the available command has worked out well. I have a big loop that reads from the network from both the server and client and without that command you were unable to do anything until something was sent over the network. I have yet to test the code on a slower machine though.

James
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!