Originally posted by Daniil Sosonkin:
One more question, maybe you can point out on technque of making sure all my data is being sent out and received? Say if I need to send out: (int)(byte)(byte)(double)(int). And at the other end I need to receive them in the same sequence. How would a receiving end know when its all received? Or how a sending end would know when its all sent out. Come to think of it, what happens if I send data too fast?
Thanx
The NIO client and server that I refered you to in my previous post uses the shutdownOutput method to ensure that the client has received all data before closing the connection. This is discussed in those topics. The only sure way to know that your partner has received your data is to implement some kind of application level acknowledgement. The standard way is to send a message requiring an ack when you are done sending data and then wait for the ack to return. In this respect NIO is no different from the blocking stream based I/O.
When it comes to detecting problems caused by sending too fast, NIO will not send all that you requested if its buffers are full. The byte count returned by write will be less than requested and the buffer will still have hasRemaining true. When this happens you must add OP_WRITE to the interstOps and not do another write until it is selected again. See those examples and the NIO Circus for working versions of this.