Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Simple NIO nonblocking server with source code  (Read 3899 times)
0 Members and 1 Guest are viewing this topic.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Posted 2008-01-22 10:57:58 »

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.

malloc will be first against the wall when the revolution comes...
Offline CommanderKeith
« Reply #1 - Posted 2008-01-22 11:09:19 »

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  Cool

Keith

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #2 - Posted 2008-01-22 12:00:30 »

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 Smiley).

malloc will be first against the wall when the revolution comes...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sunsett

Senior Member




ribbit!


« Reply #3 - Posted 2008-01-30 21:36:28 »

JGN supports straight byte[] sending/receiving. Wink

Further, it has complete UDP and TCP support in NIO....not that I'm biased.  Roll Eyes
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #4 - Posted 2008-11-27 14:54:54 »

Now moved to SourceForge:

http://sourceforge.net/projects/javanioserver/

...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 Wink).

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).

malloc will be first against the wall when the revolution comes...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (11 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!