Code request: NIO sockets

Could anybody post some code to handle non-blocking Sockets with 1.4’s NIO, as opposed to the ‘traditional’ approach of starting a new Thread for each connection (which quickly breaks under hundreds of Threads)?

A simple chat server (similar to the thousands of ‘traditionally’ written ones that are already out there) would be ideal.

Thanks.

I have that code, but too much to post here… :-[

Have you checked the common tutorials? I started with this one:

http://www.owlmountain.com/tutorials/NonBlockingIo.htm

Does not take you very far, but it’s a beginning.

Is it really going to be worth the trouble of switching to NIO rather than just having a List of Sockets and checking to see whether there is something available() before doing something with it?

The question arises when to call available()!
You won’t want a server to loop and poll, or to act in timeslices. So you have to have one thread per connection hanging in a blocking read. Buhhhh…

NIO is definitely worth the effort, together with the beautiful ByteBuffer which I think are much easier to handle than DataOutputStreams e.g.

Effort has to be taken in any case, so why not NIO?

kenrod, check the tutorial and in case problems arise beyond it, contact me privately. As I said, I have some code here willing to share, but it’s not suitable for a forum.

BTW, take care! ByteBuffers have nasty bugs in 1.4.0 (at least one). Make sure to have 1.4.1! (Took me hours to find out…)

Well, I guess that’s what I’ve got at the moment. One thread polling all of my sessions several times a second to see if there is anything available(). That’s how I answered the question :smiley: though I don’t know yet how my Session class handle under stress, so I’m interested in someone’s experience re: the difference between the two.

That is one reason I’m wary about trying to learn. I have been frusterated enough times with J2SE libraries not working quite as advertised to generally avoid using features where there are not well-documented tutorials available. I’m still a newbie! I have enough of my own bugs to worry about.

You’re a great resrouce for me :hehe:. Anything concrete/numerical you can offer up about the advantages of me switching over right now? I’m writing a game server for a game which is not very bandwith hungry (you can run two instances of the client over a 56k connection simultaneously) but I hope to be able to support 100-200 players on Pentium II-class machines. Worth the trouble of being preemptive you think?

I started from scratch and not even tried traditional Socket code. So I have nothing to compare with.

Maybe, your server need kind of a heartbeat anyway - then its completely reasonable to check all incoming lines frequently. Otherwise its a waste of CPU resources.
Depending on how fast you make your server poll the incoming lines, this might be a source of a significant additional latency. If you poll 10/s, you add an artificial average of about 50ms, which is more than the network itself might have caused.
So it really depends on your needs. If your turnbased e.g…

With NIO, you handle packages immediately when they arrive. This has other drawbacks of course, for its difficult for the server to accumulate messages before sending them out again.

opposed to the ‘traditional’ approach of starting a new Thread for each connection

Thread pool. Get 10 threads for 100 connections. NIO is nice too.