Hi, I'm Adam. Email me about: CTOs, Mobile/iOS, Project Management, and Development

Java NIO Server Example

I’ve made a small simple but complete java NIO server (with full source included) that is free for anyone to use for anything. At the moment, it only deals with sending and receiving strings, and isn’t optimized, but if anyone wants to improve it and send me the changes then I’ll post up an improved version here.

Download, documentation, license details, tutorial etc after the jump…

EDIT: I’ve now created a SourceForge project for this project – http://javanioserver.sourceforge.net/

EDIT2: Because WordPress is utterly, utterly crap at allowing you to have category pages, the old “main news page” for the project no longer exists. Blame WordPress, and hope that *one day* they will bother to fix their code so that it supports this standard blogging feature :).

Download

The most important thing is the full source code (DOWNLOAD FROM SOURCEFORGE LINK ABOVE), but if you decide just to use it as-is, you’ll probably want the much smaller binary-only jar (DOWNLOAD FROM SOURCEFORGE LINK ABOVE). NOTE: both jars have an auto-run that runs a very simple Hello,World! server so you can check they are working OK.

You’ll also need log4j (the fast, powerful, and extremely widely used open-source logging-library) – if you want the application to auto-run, you’ll need to name your log4j file “log4j-1.2.8.jar” (there’s a copy of the latest version here (DOWNLOAD FROM SOURCEFORGE LINK ABOVE), already renamed for you, that you can download directly; sorry, that was my mistake when building the JAR).

Finally, there are fully-documented javadocs here that should answer all your questions and explain how to use this library.

Why did I make this?

Mainly because Sun’s NIO libraries are good, but very low-level, with a few major flaws and a few unfixed bugs,
so that it takes a lot of time and effort to make even the simplest of working
client/server applications with them. This small, simplistic library (fewer than 1000 lines of code)
is designed so that you can:

  • easily implement a new single-threaded, non-blocking NIO server in minutes
  • edit the fully-documented source to customize it any way you need
  • read all the source code and understand how it was done so you can make your own servers from scratch

In case you weren’t aware, MOST resources on the internet providing source code
for Java NIO servers are WRONG and FULL OF BUGS that mean they WILL NOT WORK –
including BOTH of the O’Reilly books I’ve read (“Java NIO” and “Learning Java”).
That’s pretty shameful considering those are both extremely widely-used books :(.
In both cases, the authors clearly A) never read the API properly, and B) never
actually tried writing any NIO server code in their life. Or, at least, they
wrote it but never ran it on the internet – a glance at their source code shows
it won’t work except on a local switched LAN, and even then only for VERY simple
programs.

So, although this library is NOT particularly optimized (mostly in that it is
wasteful with how it creates ByteBuffers), it DOES ACTUALLY WORK and is pretty
simple and well-documented, so hopefully you can use it to make your own servers.

Why string-based, and TCP only?

TCP only: because I created this for “incubation” projects – i.e. small game
experiments I wanted to try out. The code supplied will actually work reasonably
well for most realtime games with current internet performance (I’ve made various
simple multiplayer games with it) – but if you find your game is particularly good
and want to make a proper multiplayer version over the internet, you’ll want to
replace the TCP with UDP. That will be very, very easy to do – but not necessary
for the majority of people who are just making simple games.

String-based: because A) you ALWAYS need strings! (what, your game has no in-game
chat? Shame on you!), and B) it’s much, much easier to debug, and C) when you’re
developing code very fast (I’m using Scrum) strings allow you to use “fuzzy”
message protocols, such as XML + XPath, which means that when you switch around
features, or change the message format, most of your code carries on working fine.
Binary protocols tend to break every time you make even one tiny change.

When you get to the point where strings aren’t good enough performance for your
game, you’ve already got a pretty damned advanced game, and you’ll find the only
thing you need to do to make this server work purely in bytes is to REMOVE lines
of code (all the Charset encoding and decoding) and to change one or two method
signatures to accept bytearrays or bytebuffers instead of strings as argument.


33 thoughts on “Java NIO Server Example”

Comments are closed.