I spent the entire weekend figuring out how to do real-time UDP networking for games in Java and I will share my thoughts. Some things I learned:
- Contrary to the popular assumption, real-time networking support (UDP) is something Java seriously lacks good solutions for.
- The vast majority of people recommending Java networking libraries have never used them, and therefore don't realize they are bad.
- Information regarding Java networking is extremely sparse, conflicting and incorrect at times.
Libraries I tried, and my thoughts:DatagramSocket (import java.io.*) I/O JDK Library - Looks like exactly what you need until you use it and realize it's blocking, you need to thread it, AND you are required to have seperate ports for each client you want to connect. Wtf were they thinking?
DatagramChannel (import java.nio.*) New I/O JDK Library - Extremely unintuitive and over-engineered. Highly misleading naming conventions. ByteBuffers are ridiculously unintuitive to use and cause a lot of trouble.
I eventually went with this solution after two full days of intense research, hair pulling and rigorous testing.Kryonet- Makes extensive use of "javascript style" anonymous functions. Layers upon layers of abstraction, which means you will never have simple access to your networking resources.
Apache MINA and Netty 2- Too high level if you want any sort of fine grain over your networking that is all too useful for real time networked games, although not as over-engineered as Kyronet. Apache MINA is the successor to Netty as far as I know.
extasys - If I didn't end up nearly killing myself with NIO, I would have settled on extasys. Reasonable middle ground.
java-enet (jenet)- enet for C is great, and in theory, java-enet sounds good. However it is incomplete and realistically, nobody uses the enet Java version, so you're taking a huge risk building your project on it.
priobit- You're taking a huge risk building your project on this as it looks unmaintained.
Conclusion:I can now see why there are not many multiplayer Java games. Java really needs just a basic, non-blocking UDP interface in the JDK, but it does not. Prepare to spend lots of time.