|
Over the last couple of days I've been using the NIO tutorials to attempt to implement a basic client login setup, (they send me accountname/password, I check this against my stored names and verify). Basically, my question is with regards to the following code contained in the method 'protected void processReadableKey( SelectionKey key ) throws IOException': ------------------------ int numBytesRead = rbc.read( inputBuffer ); if( numBytesRead < 0 ) { // This is an undocumented feature - it means the client has disconnected key.cancel(); closeChannel( key.channel() ); return; } ----------------- where RBC is a ReadableByteChannel. My primary problem is when the client is shut down, the key relating to that channel is a isreable() == true key that hits the line 'int numBytesRead = rbc.read( inputBuffer );' and throws a 'java.io.IOException: An existing connection was forcibly closed by the remote host'. I was under the impression that the code given should prevent this, but it just quits out of the loop and the key is constantly passed back into the iteration.
My second question is with regards to the method closeChannel(SelectionKey), above, the tutorial doesn't explain this method at any point, and I was wondering what exactly is required to close a channel using NIO.
Thanks for any response, let me know if you need further details, as it's very possible that I've just made a stupid mistake somewhere and due to inexperience havn't noticed it yet.
|