NIO for networking

Hi,

I’m about to start work on a simulation which sends its updates p2p. Depending available bandwidth a peer will have between 6 - 20 active connections.

I haven’t done any tests yet, but would NIO be helpfull here? I’ve read mixed stories; That NIO is overly complex, that a thread per socket performs better for a small number of connections than NIO, and conversely that the memory footprint of a thread per socket adds up quickly…

Thanks!

Matthijs

No, just use thread-per-peer.

Cas :slight_smile:

The fun thing is that, at least under Windows, regular sockets in Java are actually backed by about 12 non-blocking I/O workers. So the amount of threads is: yourThreads+min(needed, 12), even in this setup, it beats NIO by roughly 25%.

I get the impression from you guys that NIO is not the best for networking, is this really the case? In the game I am currently working on I poll a single non blocking DatagramChannel on every gameloop update and I have yet to run into any problems. Then again I have also yet to test across a large number of different setups. What sort of issues can I expect to run into using this method?

It’s slower, and harder to maintain.

Besides that, with regular I/O (blocking) you can use 1 thread for UDP too.

NIO comms was originally designed with tens of thousands of concurrent connections in mind, where doing it thread-per-client might well lead to scalability issues. Mostly everyone actually doing server IO is only actually doing numbers in the sub-100s and therefore better off with the easier thread-per-client model of traditional IO.

Cas :slight_smile:

Thank you. Luckily the networking transport is abstracted away nicely so it shouldn’t be much trouble to switch paradigms and give the thread per client model a whirl.

one connection per tcp-client, in the udp world it makes no sense.

Sorry that’s what I meant! :slight_smile:

Thanks!
Confirms my doubts, with that out of the way, does anyone know a good networking (p2p) library? Ironically most of them seem to be build around NIO (and client/server for that matter). I know about JXTA, but it’s way too bloated and over-engineered for my taste.

Edit: Just found this lib (http://code.google.com/p/ice4j/) which should take care of most p2p NAT traversal issues.

Throw in UPNP & NAT-PMP and you should get a half decent punch through success rate :slight_smile: