Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (754)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  A Couple Questions with regards to the NIO tuts  (Read 3185 times)
0 Members and 1 Guest are viewing this topic.
Offline DeathofRats

Junior Newbie

Java games rock!

« Posted 2004-06-02 20:06:20 »

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 = inputBuffer );
           if( numBytesRead < 0 )
                 // This is an undocumented feature - it means the client has disconnected
                 closeChannel( );
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 = inputBuffer );' and throws a ' 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.
Offline blahblahblahh

JGO Coder

Medals: 1

« Reply #1 - Posted 2004-06-02 20:43:20 »

I just wrote a long and detailed response to this. Unfortunately, Mozilla bitch-slapped me (stupid moronic key shortcuts handling, and whilst I was trying to fix what it just did to me it crashed before I could hit "Post") and I haven't the time left to rewrite the whole thing.

So, in brief:

- part of the problem is that the tutorial code is actually wrong. Trust me, I wrote it Grin
- this is Sun's fault, not mine. They have NOT documented what "actually happens" nor "how you should use this". As a result, I had to use trial-and-error to work out many of the details. Their JVM was broken (1.4.1 and also the first 1.4.2 IIRC) and I accidentally coded these tutorials to work with the BROKEN behaviour of specific versions of Sun's JVM. Because they didn't document this, I didn't find out until some aspects of our production code stopped working when we tried upgrading the JVM.
- ...but it didn't hit us hard, because the real code that I used as a basis for the tutorial has a LOT more defensive programming in it, most of which I stripped out for the tutorial to make it clearer and easier to read. None of it was "essential".
- However, until now I completely forgot that the tutorial would need updating too!

Specific fixes:
1. In the block where you deal with the "undocumented feature" which is still in the latest Sun JVM (and you HAVE to support even though it's undocumented), I have a better approach now. Instead of the code in the tutorial, just use the line:

throw new ClosedChannelException();

2. ...because Sun has fixed their JVM, which used to NEVER throw this exception, but now it does. Often. So you have to catch it anyway, and if you're doing that, you might as well use it. However, they haven't quite fixed all their bugs, because there are occasions when you get the -1 read INSTEAD of a ClosedChannelException - but I *believe* from reading between the lines Sun *intended* to throw that exception. Hence my recommendation that you do it yourself.

3. Modify your code to catch those CCException's. It's pretty obvious what causes them - the other end of the connection has disappeared for any of a variety of reasons, so take whatever actions are appropriate in your game when someone disconnects.

I would almost bet money that doing this will catch that IOException you are getting Grin.

If you follow the above advice, you'll be deleting the "closeChannel  ( ... )" code, but FYI since you asked I just had a pretty boring method that did little more than Channel.close().

The reason I *had* the method is that there have been changes from version-to-version about what happens when you try to close channels - and, again, Sun has failed to adequately document what you ought to do. So I had that method so I could tweak it each time they changed the JVM behaviour Smiley. Nowadays, I find that just catching the CCExcepiton is enough, and don't normally need to close anything manually.

EDIT: two more things to add.

1. The above advice may be wrong. If a Sun engineer came to this forum and said so I would be overjoyed. For the 3 years or so that NIO has been around, I have never seen or heard of any human who claims to know how NIO works (and I've scoured the web many times looking for all the info I could find anywhere on NIO). I think Sun decided they "knew too much" and executed them as soon as they'd finished their work, just like Pharoahs and their pyramid-builders.

I'd be even happier if he/she also explained WHY and gave us some documentation for this API!

2. I haven't bothered reading the 1.5.x API docs yet - I'm waiting until 1.5 goes gold (at the moment it's still beta and subject to change). It could be that there are wonderful detailed API docs for NIO that explain all this.

If so, my apologies for misleading you. However, there are several servers on the net at the moment using my NIO libraries that have been serving games for months at a time without restarting the JVM, so you can rest assured I actually use this stuff in real life Smiley. Unlike Ron Hitchens (author of the ONLY NIO book) whose book is a nice introduction to NIO, but makes it look like he's never used the API himself!

malloc will be first against the wall when the revolution comes...
Offline DeathofRats

Junior Newbie

Java games rock!

« Reply #2 - Posted 2004-06-02 22:55:19 »

Excellent, my code works now as it did with using threads, thanks for the quick reply, and I anticipate when you get around to finishing your tutorials. I'll certainly keep them bookmarked and check back again.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MickeyB

Senior Devvie

my game will work, my game will work!

« Reply #3 - Posted 2004-11-22 17:41:28 »

Which tutorial is this?       Grin


Current Project:
Offline blahblahblahh

JGO Coder

Medals: 1

« Reply #4 - Posted 2004-11-22 19:02:57 »

(halfway down page)

malloc will be first against the wall when the revolution comes...
Pages: [1]
  ignore  |  Print  

DesertCoockie (20 views)
2018-05-13 18:23:11

nelsongames (68 views)
2018-04-24 18:15:36

nelsongames (65 views)
2018-04-24 18:14:32

ivj94 (748 views)
2018-03-24 14:47:39

ivj94 (79 views)
2018-03-24 14:46:31

ivj94 (579 views)
2018-03-24 14:43:53

Solater (95 views)
2018-03-17 05:04:08

nelsongames (168 views)
2018-03-05 17:56:34

Gornova (362 views)
2018-03-02 22:15:33

buddyBro (1022 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!