Networking Design

Junior Spellweaver
Joined
Sep 9, 2012
Messages
145
Reaction score
7
Recently I thought I'd try to make a sort of Maplestory-like game and have gotten some good progress with just basic combat, platforming, mob spawning, NPC's, and even quests. This was all fun but I really wanted to do this to learn a lot more about networking with Java. I looked up some Apache Mina tutorials and followed up on that and it sort of works but I was just wondering if someone could explain to me the whole design behind the standard network design in most Java repacks. My game is very slow with more than 5 people connected and since all of the logic is handled server-side I think I'm not optimizing it well enough or doing it as well as Odin. Basically I take the client's input and process that through a Player object which then moves position, attacks, etc. and can interact with other entities. Every frame a packet is sent to each client full of things to render based on entities in the corresponding map. Any suggestions, tips, advice would be very helpful and appreciated.
 
Networking in Java is pretty much the same as networking in any other language. You're basically going to deal with a read and write buffer. It would be stupid of me to paraphrase everything about networking (even if I specify it down to Java, which you really can't if you don't know the fundamentals of networking) into this one post. There's so much to talk about when it comes to networking - they have hundreds of courses and certificates for this type of stuff. Really, all you have to do is search "networking with java" or "server client networking" That should give you enough details on what you need to accomplish.

Also, it looks like you have some idea of how packets work. They work mainly the same way for every client-server model (the structure of the packet is user-defined)

Like I said, I'm not going to paraphrase what you can find on Google yourself. That's basically how I learned about networking.
 
Whenever you are coding something of which you know it's going to be called often, make sure it is optimized.

"Every frame a packet is sent to each client"
This shouldn't have to be the case for any type of server-client architecture. ONLY send the necessary packets to the necessary clients. Sticking to that principle will already greatly improve your way of thinking.
 
If you need high performance networking, check out Netty (http://netty.io) - it's fast, scalable and lightweight.
Here's a good primer into the problems with networking in Java & how Netty addresses them -http://netty.io/wiki/user-guide-for-4.x.html
If you are using a traditional approach (e.g a thread-per-client model), I'd suggest looking into Netty.
 
To put it simple:
Connection.Receive => Protocol.Translate => Handler.Handle => Protocol.SendResponseTypeX => Connection.Send

To put it simple, the connection receives raw data which the protocol translates the data into commands, for the handler to handle the command, then sends it back to the protocol to send a response to the connection.

Don't know what type of server is(multithreaded or NIO), my opinion is to use NIO, its hard to learn but it is has its advantages, and can handle more then 1000 clients(with threads or thread pools, look into Java Concurrency, the CPU performance drops with each thread created).

- Make your packets small and fast to process,
- Load your game world data into your application memory(since that data is constant and never changes, less time spend on queries)
- Make Fast accessible algorithms to access data much faster(Try to sort the data on initialization, for a quick search);
 
How do Maplestory private servers have almost no lag with character movement? How many times per second are movement packets sent? I think my main problem is trying to figure out how fast to send packets and then try to interpolate between last frames x/y and this one's to make smooth movement.
 
Back