1

I'm doing small game Tron Bikes.

I have MainFrame class

Bike bike1, bike2;
private void startGameAsServer(){
    bike1 = new Bike();
    bike2 = new Bike();
    communication.sendBike(bike1);
    communication.sendBike(bike2);
}
...
public synchronized void receiveBike(Bike receive){
    //depends if I'm server or client I save receive to bike1 or bike2
}
...

Second class Communication

...
ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream input = new ObjectInputStream(client.getInputStream());
...
public void sendBike(Bike send){
    try {
        output.writeObject(send);
        output.flush();
        System.out.println("Send bike");
    } catch (IOException ex) {
    }
}
...
private void listen(){
    listening = new Thread(new Runnable() {

        @Override
        public void run() {
            while(true){
                try {
                    Bike readBike = (Bike)input.readObject();
                    System.out.println("Receive bike");
                    MainFrame.getInstance().receiveBike(readBike);
                } catch (IOException | ClassNotFoundException ex) {
                    break;
                }
            }
        }
    });

    listening.start();
}
...

When I start game, it works fine. On server both bikes are created, send to client, and then game start. But when in client I turn to side, I send one bike back to server, and here is problem.

In server in method receiveBike bike1 and bike2 is null... And if I write to bike1 or bike2 nothing happend. Even many times in a second, data from bike1 a nd bike2 are loaded to draw game panel. It looks, that data are loaded from old objects.

Like in method receiveBike we change completely different objects... I'm so desperate, I don't know what I'm doing wrong...

3
  • Why sendBike() is static ? Can you post code of this method? What is input in run() method? Commented Dec 19, 2013 at 17:41
  • The fourth line of the startGameAsServer(), did you mean bike2, not bike1? Commented Dec 19, 2013 at 17:47
  • Sorry, edited. Yes I mean bike2, and sendBike isn't static, there should be small 'c' in connection... Commented Dec 19, 2013 at 19:35

1 Answer 1

3

You need to call ObjectOutputStream.reset() or use writeUnshared(), for the reasons given in the Javadoc. The stream is conserving object graph integrity by giving you back the same object every time. These methods circumvent that.

NB you need to catch EOFException separately and break out of your reading loop when you catch it. Any other exception should be logged.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but this doesn't solve my problem.. My problem is, that in method receiveBike, which is called from thread in funciton listen, I can't see variables bike1 and bike2, and I write to them, other functions from MainFrame see old ones, but thread in this function see "new" one, null for first time and then what I wrote... I set volatile for bike1 and bike2, synchronized for method but it still doesn't work...
What does "can't see" mean? Is there perhaps a variable scoping problem?
Sorry, bad implementation of your help... "Cant see" I thought, that in bike1 and bike2 is null, but before it it was initialized. I don't understand why. Now sending works great from server to client. But not opposite way. But thank, now I move little

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.