I’ve just posted: http://tmachine1.dh.bytemark.co.uk/blog/index.php/2008/01/22/java-nio-server-example/
(very simple non-blocking NIO server with fully documented source)
I needed something very simple I could use with Slick to write multiplayer games in under 16 hours each (i.e. one weekend at a time), and simple and plain enough that I could easily change it, replace it, fix it, etc.
NB: this is not intended to replace or compete with the existing full-fat java networking libraries people have written. This is the simplest possible thing I could come up with, and you’ll probably find it more useful for teaching yourself NIO than for anything else. But, that said, I am actually using it in mini games including one I’ve already written (will post that game sometime in the coming weeks).
What does it provide?
1. a very simple network protocol (4-byte message length followed by message)
2. a single-threaded NIO server that copes with fragmented packets properly (took a lot of testing, but I *think* it works in all cases now)
3. a simple Charset-based decoder to convert all messages into bytes and vice versa
4. two abstract methods on the server that allow you to a) receive any incoming messages and b) perform "server-side game tick stuff" after each Selector.select
5. a tiny client that connects with NIO and implements the protocol
Simple stuff that most people have written for themselves by now.
I thought this might be a good tutorial for people who want to know the simplest possible example of getting NIO to send and receive strings ! (and … at 500+ lines just to achieve that, I think I’ve underlined quite how low-loevel NIO really is!)
Fairly detailed javadoc here:
http://tmachine1.dh.bytemark.co.uk/nioservers/javadoc/
NB: it’s not very good, but I was experimenting with following the rules of Scrum correctly (i.e. make it work “just good enough” in the absolute minimum possible amount of coding time).
There is one known bug that you are unlikely to encounter, which is documented with a FIXME, and requires copy/pasting some source from the server’s message parser into the client’s message-parser, but other than that it seems to work well enough - I’ve done a multiplayer tetris clone that worked fine with it.
Some time in the future, after I’ve used it a bit more in quick-n-dirty personal projects, I’ll release an update. Or sooner, if anyone sends me fixes.