Textbook for Java NIO Networking.

I’ve been using Netty for quite some time, which is built on the Java NIO framework, but as I enjoy tearing down into the core of things, I’m wanting to learn how to write my own applications using the Java.NIO package.

I’ve been looking really hard for decent study material on this, but it seems like anything pertaining to is either low-quality, or very low in quantity. Most of the ‘tutorials’ I find online don’t explain anything, and while I can write a basic server by now, I don’t really understand how it all comes together.

My over-all goal is to move my server-api to NIO from Netty with the next major release, but to feel confident enough to do this, I will need to do a lot of studying. I don’t want to degrade what I have, but I also don’t like the feeling of relying on someone else to do all of the hard stuff for me.

Any suggestions are welcome, would prefer if it at-least uses Java7, but J8 is recommended.

Price doesn’t matter, only quality. I’m willing to spend $250~ on a good NIO networking textbook.

I don’t know if it can help you, but i studying more deeply nio some years ago and i started to write a server api available here : https://code.google.com/p/objectserver/
It’s quite functionnal but it was mainly for studying purpose. The code is quite well documented (if i remember well, you should start from EventDispachThread class).

I don’t recommand to use it. I don’t maintain or use it !! now i understand how it works, i use netty :stuck_out_tongue:

But for books materials, i have no advices to do… sorry.

Thanks, I’ll look through this, don’t get me wrong, I use Netty too, and I understand it quite well, well, how to use it, not how it works. I can write a server using Netty and have it perfectly functional, but it just doesn’t satisfy my lack of understanding.

nio is loosely based on a non blocking C api. Most people don’t like it, and have found it buggy. I however didn’t really have any problems and only had to spend a day with the docs to write a pretty good network layer for my stuff.

These days with at least unix OSes and linux in particular large numbers of threads with the old blocking api works very well, and last but not least green threads from riven can also do this even more efficiently across platforms. You really need to consider the problem you are trying to solve. If it is for very highly scalable servers, then i would argue that perhaps your doing it wrong for an indie game. And if you still want to do that then well i would say try green threads first.

nio had and probably has its place. Just not for most of us.

I’m curios to find out more about why you think its buggy? For file base operations i have found it to be pretty stable, one of the benefits it that it allows you to have direct allocation buffers.

My personal gripe with NIO is that after a while (minutes, hours, sometimes days) Selector.accept() starts to immediately return instead of block, even if there are no I/O events (no selected keys). This is clearly forbidden by the spec. All NIO libraries have workarounds for it, so at least I know I’m not the only one. :persecutioncomplex:

My guess is that when you de-register interest ops that just happened to have triggered on one of NIOs threads (on Windows, NIO has 1 thread for every 64 connections - on Linux it’s one thread), that NIO will schedule the key to be selected, making accept() return immediately, but then it will not pass the key to the public API, as the interest ops say it should be filtered out.

Result: suddenly your network-loop is taking 100% CPU usage of one core, and suddenly it stops.