Jump to content
  • Advertisement
Sign in to follow this  
justkevin

Should I modify my server code to use Java NIO?

This topic is 3178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a working prototype of a game server (Java) and client (Flash). I'm currently using an architecture that creates a thread for each game instance (or "Sector"), and a thread for each client connection. The connections receive data from the client and pass these commands to the sector. The sector does its thing every 200 ms and sends updates to the clients. All this works fine, but very preliminary testing shows the java server starting to strain at a couple hundred simulated dumb clients. VisualVM indicates that the server is spending most of its time in the network related methods which are mostly doing things like writing to a DataOutputStream on the socket. Questions: Is it likely the performance degredation is the result of blocking? Would rewriting the network code to use non-blocking NIO sockets help here? Finally, if the answer to these questions is "yes", can anyone recommend a good explanation of how NIO sockets work and how to use them? (the documentation I've looked at so far seems... intimidating) Thanks!

Share this post


Link to post
Share on other sites
Advertisement
In general, assigning a thread per connection doesn't scale well because the operating system eventually has to spend so much time switching between threads that it doesn't have much left for actual processing. A project I worked on initially used this model, but after we discovered that it didn't scale beyond a few hundred simultaneous connections at best, we switched to a solution based on Boost.Asio. While I'm not very familiar with the networking facilities that Java provides, from what I've heard NIO is the way to go.

Rewriting the network code could quite possibly yield a significant increase in server performance. There are examples of single server machines that serve thousands of players all at once (I believe it was some UO emulator?). This can be achieved with cleverly written network code.

Share this post


Link to post
Share on other sites
NIO is most certainly the way to go.

It's not too difficult to use but there a few gotchas for the uninitiated. Adam from T-Machine has a good example here, that should be a good starting point for you.

You will however lose the use of the DataOutputStream, since NIO uses ByteBuffers, but that shouldn't be too much of a problem.

Regards
elFarto

Share this post


Link to post
Share on other sites
Good to know, it sounds like moving to NIO is the way to go. From reading various blogs, forum posts, etc., it sounds like using NIO correctly is a black art. The T=Machine post indicated that even the authors of Java NIO by O'Reilly made serious mistakes.

Should I try to implement my own NIO messaging, or would I be better off spending my time learning an NIO framework like Netty or Mina?

I'm normally someone who likes to roll-my-own for purposes of understanding, but time is finite and this is a large and ambitious project.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!