Simple NIO nonblocking server with source code

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.

Hi Adam,

Happy new year!

Sounds cool, I would have loved to use this. Right now I’m looking for something with UDP as well though so I’m looking at MINA.

Something bare-bones that just sends byte arrays is definitely needed and will be used - I struggled for ages to find a mediocre implementation. For some reason most frameworks (including MINA grrrrh) try to do heaps more than byte-array send and recieve (they try to enforce your way of doing threads as well as message to byte array conversion and vice versa).

Great work 8)

Keith

I’m going to be needing UDP myself in the near future I’m sure - but so far, for simple casual games written quickly, it has been “OK” just to use TCP. shudder.

(…but it’s interesting following the scrum / agile / “only make exactly what you need right now and no more” to it’s most extreme :)).

JGN supports straight byte[] sending/receiving. :wink:

Further, it has complete UDP and TCP support in NIO…not that I’m biased. ::slight_smile:

Now moved to SourceForge:

…and I’ll be doing an SVN check-in (once I can get my Mac to connect to the SF SVN servers) so you’ll be able to pull the source out via SVN if that’s your preference (hint: I’d strongly suggest this is a good idea ;)).

If anyone’s been using this and has made any improvements they want to share, send SVN commits via sourceforge (I’ll add anyone to the project as necessary).