0

I try to make a card game.I have a thread problem. I construct objectinputstream only once and then just send a reference to it to the other Thread and two thread try to read an object from same objectinputstream at the same time.The same problem is java.io.StreamCorruptedException: invalid type code: 00 but it didn't solved.

ClientService Thread:

    while (true) {
        try {
            Message message = (Message) in.readObject();
            handleClientMessage(message);
            if (message.getType() == Message.messageType.ClientDisconnect) {
                break;
            }
        } catch (IOException ex) {
            Logger.getLogger(ClientServiceThread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientServiceThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Game Thread

public void run() {
    startGame();

    while (true) {
        try {
            Message msg = (Message) inPlayer1.readObject();
            handlePlayerMessage(msg);
        } catch (IOException ex) {
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

in and inplayer1 the same objectinputstream.I get an exception : java.io.StreamCorruptedException: invalid type code: 00 .Two classes make a different job only this part is similar.Please give an advise how I solve this problem.Is there any way ?

0

2 Answers 2

4

Just have one thread reading the stream and doing everything which needs to be done for each object.

This thread can do all the work of the two threads and/or you can have it message between threads to have other threads do additional work.

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

Comments

1

If ObjectInputStream is constructed only once and then just passed a reference of it to the other Thread then simply enclose the access of this object inside synchronized block to make sure that only one thread can access this object at a time.


Assumption: both in and inPlayer1 are representing to same object that is actually a ObjectInputStream and shared between different threads.

ClientService Thread:

... 
Message message = null;
synchronized (in) {
    message = (Message) in.readObject();
}
...

Game Thread:

... 
Message msg = null;
synchronized (inPlayer1) {
    msg = (Message) inPlayer1.readObject();
}
...

2 Comments

thanks but with this way only one thread read the message(Sometimes gamescreen sometimes clientservice) I want to both threads reach the same message at the same time .
you haven't mentioned this thing. No worries at least it will solve the StreamCorruptedException.

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.