views:

80

answers:

2

Firstly I know coordinates is probably the wrong terminology? I'd assume i'd simply be passing the x and y variables, but coordinates describe it better I feel.

Now I need to have a Server which can be accessed by 2 clients, it is a racing game and it requires each client to be able to maneuver a racecar simultaniously, each using a different control scheme but that's neither here nor there.

I was hoping someone would be able to assist me when it came to sending the x and y positions of a racecar to the server and having the server send them onto the next player and vice versa to allow both racecars to move at the same time on each clients window. So far i've only done the simple server stuff, such as the knock knock server on the sun website, and a simple echo server which repeats a string I send to the server.

When I tried to use int instead of string I recieved an error that the int I wanted to pass was dynamic (obviously changes with each movement) and cannot be passed as static (using readInt and writeInt).

So any help on how to create the wanted movement on both client windows through the server would be appreciated.

Thanks

A: 

A simple solution is to use ObjectOutputStream with serializable objects:

class Coordinates implements Serializable{...}

ObjectOutputStream out = new ObjectOutputStream(...);
out.writeObject(new Coordinates(...));
Thomas Jung
Thank you :) a bit of research into serializable objects yielded some good results.
Craig Jones
A: 

Craig, you're close. Dynamic/static is a misunderstanding, this has nothing to do with your racing game or values changing. It's just that you can't deserialize fields, that are declared static (or transient), as it says in the javadoc for ObjectInputStream:

Fields declared as transient or static are ignored by the deserialization process.

The solution is (hopefully) simple: remove the static modifier from your x/y integers, that should solve the problem. btw - if your String had been static, it would have failed exactly the same way.

Andreas_D
thanks for the suggestion, unfortunately the integers were called upon in main, and removing the static cause an issue when running that it could not find a main method. I think serializable objects mentioned above is the right track for what I need. Thanks again though.
Craig Jones