Adrian Jandl
Adrian Jandl

Reputation: 3015

StreamCorruptedException for Server-Client app

My Server runs this code in a Thread, so that I can interact with the GUI while I am waiting for my client to connect.(I am only using 1 client and 1 server at the moment.)

@Override
public void run() {

    Socket client = null;
    MessageListener listener = null;

    try {
        client = server.accept();
        oos = new ObjectOutputStream(client.getOutputStream());
        ois = new ObjectInputStream(client.getInputStream());
        listener = new MessageListener(ois);
        (new Thread(listener)).start();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

My Client runs this code once to connect to the server and fix the input/output streams:

try {
        ois = new ObjectInputStream(server.getInputStream());
        MessageListener listener = new MessageListener(ois);
        oos = new ObjectOutputStream(server.getOutputStream());
        (new Thread(listener)).start();
    } catch (IOException e) {
        e.printStackTrace();
    }

MessageListener runs a thread to check for incomming messages.

@Override
public void run() {
    while (true) {
        Message message = null;
        try {
            message = (Message) in.readObject();
            if (message != null) {
                System.out.println(message.getName());
            }
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }

    }
}

However, when I launch my program, at first I can only send from Server to Client and not the other way around(I just cannot interact with the Client GUI). Once I send the first message from Server to Client I receive the following error message:

java.io.StreamCorruptedException: invalid type code: 6F
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.jale.chat.iohandling.MessageListener.run(MessageListener.java:25)
at java.lang.Thread.run(Unknown Source)

Edit: after I have gotten the first error message, I can only send from Client to Server(the sending actually works) but the Error messages keep spamming my console.

Line 25 of MessageListener:

message = (Message) in.readObject();

Haven't found anything on google/Stack Overflow, as the general Problem when getting a StreamCorruptedException seems to be when people open multiple InputStreams or OutputStreams for a single Socket, which I am however not doing. Any help would be greatly appreciated.

Upvotes: 0

Views: 366

Answers (3)

Lukas Leitinger
Lukas Leitinger

Reputation: 631

I think you might have opened your Input/Output-Streams more than once. Try checking that the connect-Function gets only called once.

Upvotes: 1

user207421
user207421

Reputation: 310860

You are constructing ObjectOutputStreams and then throwing them away. They should be passed to the constructor of MessageListener just like the ObjectInputStreams, so it has something valid per-client to write with. You can't possibly write correctly to the peer the way your code is now. Probably you are reusing an ObjectOutputStream for the wrong peer somewhere.

Those ObjectInputStream and ObjectOutputStream variables you are creating should be local to the method, not members of the surrounding class. And in fact both those streams should be constructed in the run() method of the thread you are about to start, not inline in the accept() loop. Otherwise you risk blocking in the wrong place. And the ObjectOutputStream should always be constructed before the ObjectInputStream, to make deadlocks impossible.

Upvotes: 1

KernelPanic
KernelPanic

Reputation: 2432

Hmm, you must use identical class (with id) for both client/server sides. I've put my message mechanism into library that is used in both client/server projects. And besides, if I compile server/client, I compile all three projects ... Maybe this will help.

Upvotes: -1

Related Questions