• Advertisement
Sign in to follow this  

[java] Java Game Networking

This topic is 3934 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

Hey, I'm currently working on a MMORPG in java. Here's a little screen shot to give you an idea betatownhall2.PNG I'm currently in internal beta testing with the game and I'm having issues that suggest to networking will have to be upgraded. What I'm currently doing is I wrote a basic "packet" class that every type of packet extends. For example when a player moves, the object movement packet is serialized, checked by the server and sent back across the map on the player is on. While I try to keep the data inside my packet class limited only to required information like the x/y I'm afraid that Java adds a lot of overhead when serializing a class over the network. I've had no problem with 5 concurrent user but it's hard to extrapolate from such limited data. I'm just wondering if there are any alternative that are more efficient or have a good efficiency and simplicity of code ratio. The bonus with relying heavily on object serialization is that I could probably used it to serialize the map object, which contains all require referenced, and be able to migrate maps across server with only a small delay which is useful for load balancing. Adding UDP communication would be a good way to handle some things like movement more efficiently but how do you sent to a user behind a router since there is no connections? Most MMORPG don't require to open ports so I wouldn't want this one to do it as well.

Share this post


Link to post
Share on other sites
Advertisement
You are correct. Serialization adds a lot. Here is s ample class:


import java.awt.Image;
import java.io.Serializable;

public class Player implements Serializable {
private transient Image image;
private float x, y;
private transient String name;

public float getX() {
return x;
}
public float getY() {
return y;
}
}

I ran a test to write this as a file to the harddrive. I then just wrote the variables x and y to a separate file. Even correctly marking items that will not be serialized, this class creates an object file that was 43 bytes. Just writing the x and y varaiables took 8 bytes. So if you wanted to update a single player 20 times per second, you would have to send 860 bytes with serialization and 160 bytes just sending the required information. Add 4 more players, then 4300 vs 800. And that doesn't include any other updates you might be doing. It would be wise to drop the serialization and just send what is needed.

Share this post


Link to post
Share on other sites
Java serialization is completely type-safe, something you do not require for a game. The beauty of Java's approach in general application development is that you can send a type that client doesn't have loaded, the class will be looked up in class path, constructed and instantiated. Even if before then the VM didn't even know such class exists.

Even more, you can send class definition over the network, generate byte-code from this definition on the receiving end, instantiate that, and add custom functionality.

If you wanted, you could stream actual code.

That aside, writing custom serializer (much easier than C/C++, due to automatic garbage collection and reflection) will allow you to transfer nothing but relevant data.

As for performance - it's a non-issue here. You will max out your bandwidth far before the CPU load becomes the problem. Also, clients will require more bandwidth to connect to you.

When sending information to multiple clients, you should also serialize once, then send that generated class over the network. But that should be obvious.

Using UDP will require you to re-invent the whole network stack (something you already have with TCP), but will allow you to gain full control over network traffic.

But scalability will always be limited by network bandwidth and in-game logic - serialization is a constant factor operation, no matter how fast, it's not what will limit your scalability.

Share this post


Link to post
Share on other sites
Serialization is one of those things that looks attractive but is
really a trap for this kind of application. One problem is that
it's not selective. It looks great where each user has 2 attributes.
What about when your user has 20 attributes, or 200? Another problem
is that it's opaque - if something is going wrong in your communications,
how will you figure out where? Another problem is that slightly different
versions of client and server won't be able to communicate. Another problem
is that the overall behavior of the system (all messages from/to all players)
will be impossible to log, and therefore impossible to debug.

Share this post


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

  • Advertisement