Question

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

Was it helpful?

Solution

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top