Reputation: 49
Okay, I've been working on this for days now and I can't find any information anywhere. I made a simple game that works pretty well, but I wanted to add two-player functionality. I've already established a connection to another computer over LAN, but I have no idea how I'd send the data. I understand writing to ObjectOutputStream's, but how do I get the other side to interpret it? Do I send each variable separately? As of now, I'm attempting to send an array for the enemies, player position, score, and lives. How can I send and receive it in a way that the client will understand? Thanks for your help, and I'm sorry if what I'm asking isn't clear. I'm not sure how to phrase it.
Upvotes: 2
Views: 129
Reputation: 52205
It really depends. I think that it is a good idea to just send streams of text data, this will allow you to better debug your application and also make it possible to other applications written in other languages to interface with your own application.
You could take a look at XStream (tutorial here). This will allow you to easily turn your data into XML format and send it.
For clarification, you still, will, eventually send the data as bytes. But in my suggestion, the bytes will represent strings and not serialized Java objects.
EDIT: As per your comments to the other question, you could do this:
public class GameDataProtocol
{
private Enemy[] enemies;
private Point position;
private int score;
private int liveCount;
//Constructors, Getters and Setters.
}
You would populate the class above with your information, encode it in XML and send it over to the client. The client will decode it and update the data accordingly.
Upvotes: 2
Reputation: 13097
You need serialization. This means converting your Java class containing the information into a bunch of bytes. Those bytes can then be transferred to your client where it will be deserialized to get back the original Java class holding the data you would like.
In Java, most of this is done automatically for you. You can start by looking at this.
Upvotes: 3