This week's book giveaways are in the iOS and Features new in Java 8 forums.
We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line!
See this thread and this one for details.
The moose likes I/O and Streams and the fly likes Command Stream Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Command Stream Handling" Watch "Command Stream Handling" New topic

Command Stream Handling

Arnout Boks

Joined: Mar 28, 2005
Posts: 2
I'm working on a client application that sends and receives commands (Strings) over a Socket. I have some difficulties with the command-handling however.

The problem is as follows:
There are two types of commands:
* commands that we have to send to the server, and then wait for a reply. The Thread that sent the command must stop meanwhile, like 'blocking I/O'. The replies must be passed back to the code that sent the command.
* commands that can be sent to us, without having asked for them (like an asynchronous connection). The server can send them at any time. These commands must be passed to a sort of 'Listener', that can handle them.

The order the commands arrive is completely random (for example, a asynchronous command can arrive between another command and it's 'answer'). Also, two Threads must be able to 'send and wait for a reply' at the same moment, without receiving the wrong answer.
I have methods for reading exactly one command from a stream, and for recognizing which commands and replies match.

What is the best (and most stable) way to implement this in Java? Can the new I/O classes be used to improve this?

Thanks in advance.
I agree. Here's the link:
subject: Command Stream Handling