2

I am reusing ObjectOutputStream to send updates between the two clients, this is the server code,

 public void run() {

    try {
        toPlayer1.writeBoolean(true);
        toPlayer1.flush();

        while (true) {

            try {
                found = (boolean[][]) fromPlayer1.readObject();
                player1Int = fromPlayer1.readInt();

            } catch (Exception ex) {
                // Handle  exception here...
            }

            if (isWon(player1Int)) {
                toPlayer1.writeInt(P1_WON);
                toPlayer1.flush();
                toPlayer2.writeInt(P1_WON);
                toPlayer2.flush();

                sendMove(toPlayer2, found, player1Int);
                break;
            } else {
                toPlayer2.writeInt(CONTINUE);
                toPlayer2.flush();

                sendMove(toPlayer2, found, player1Int);
            }

            try {
                found = (boolean[][]) fromPlayer2.readObject();
                player2Int = fromPlayer2.readInt();

            } catch (Exception ex) {
                // Handle  exception here...
            }

            if (isWon(player2Int)) {
                toPlayer1.writeInt(P2_WIN);
                toPlayer1.flush();
                toPlayer2.writeInt(P2_WIN);
                toPlayer2.flush();
                sendMove(toPlayer1, found, player2Int);
                break;
            } else {
                toPlayer1.writeInt(CONTINUE);
                toPlayer1.flush();

                sendMove(toPlayer1, found, player2Int);
            }
        }
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

private void sendMove(ObjectOutputStream out, boolean[][] found, int score) throws IOException {

    try {
        out.reset();
        out.writeObject(found);
        out.writeInt(score);
        out.flush();
    } catch (Exception ex) {
       // Handle  exception here...
    }
    out.writeInt(score);
}

the problem seems to be that some messages are not being delivered correctly, any suggestions? Am I using the flush() correctly? I have added reset(); it is still not working

update, these are the streams: public void run() {

    try {
             toPlayer1 = new ObjectOutputStream(player1.getOutputStream());
             fromPlayer1 = new ObjectInputStream(player1.getInputStream());
             toPlayer2 = new ObjectOutputStream(player2.getOutputStream());
             fromPlayer2 = new ObjectInputStream(player2.getInputStream());

regards, c

Stephan
  • 41,764
  • 65
  • 238
  • 329
user3131312
  • 55
  • 1
  • 3
  • 10

1 Answers1

1

If you want an object or objects to be sent again, you need to call reset() on the ObjectOutputStream object.

The problem that reset() solves is that when you send an object in a object stream, the protocol attempts to preserve object identity. The first time you send it, the stream sends the object state. Subsequent times, it just sends a marker that says (in effect) "use this object that I sent you previously".

The reset() method says (in effect) to the ObjectOutputStream ... "forget about all objects that I sent previously".

So if you want to send the same object twice, you need to do something like this:

  out.writeObject(found);
  // change the state of 'found'
  out.reset();
  out.writeObject(found);

Note that this doesn't affect primitive values sent using their corresponding write methods. Primitive values don't have "identity" and are sent literally each time.


I should also point out that the following is very bad practice.

        } catch (Exception ex) {
        }

You are silently ignoring all exceptions. This is lazy and dangerous, and you are likely to come to regret it. (Don't do it even in sample code in SO Questions ... 'cos someone might copy your bad code or some Java beginner might emulate your bad habits.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I tried, reset() but it is not working correctly, do I call reset before every writeInt and writeObject? – user3131312 Dec 29 '13 at 02:19
  • You are using `reset()` correctly in your sample code now. If it isn't working, the problem is something else. (Maybe your bad exception handling is coming back to bite you.) – Stephen C Dec 29 '13 at 02:33