Eveli
Eveli

Reputation: 498

getting StreamCorruptedException

I am trying to stream an ObjectOutput, but i get the error above.

CODE:

private void writelogin(int i, int j, int k, int c4, int l, int m, Socket sock) throws IOException, InterruptedException {
    ObjectOutputStream dos = new ObjectOutputStream((OutputStream)sock.getOutputStream());
    boolean data = true;
    int[] btw = new int[]{i,j, k, c4, l, m};

    do {
        ((ObjectOutput) dos).writeObject(btw);
            data=false;
        } while (data);
            dos.flush();
            dos.close();}}

I have no clue why.

The exception is outet via System.out not via Errorlog.

What my Server receives: AC ED (sometimes a lot of 00 and other hexa 'seem-random' numbers)

What my Stream has to send: 03 96 144 54 79 05

what my Server has to receive: 03 60 90 36 4F 05

that hexadecimal numbers are permitted is correct, so not the problem.

Problem is there, since i use ObjectOutputStream and an Array, as i used DataOutputStream and put every block itself via dos.writeByte(i);dos.writeByte(j); it worked fine.

figured something new out: my ObjectOutputStream sends an AC ED 00 05 on his own... does anyone know how to avoid that? I´m sending nothing, but he does.

Doesn't matter anymore, found a way to work around. Figured out that it is even possible to send an Array via DataOutputStream, so this is avoided.

Upvotes: 1

Views: 1050

Answers (2)

user207421
user207421

Reputation: 310860

You must use the same ObjectInputStream and ObjectOutputStream for the life of the socket, at both ends, rather than creating one or the other every time you need one of them.

Upvotes: 1

Vikram Singh
Vikram Singh

Reputation: 1806

Following code looks suspicious:

((ObjectOutput) dos).writeObject(btw);

Simply try,

dos.writeObject(btw);

Why are you typecasting and that too to ObjectOutput?

EDIT: I think you are forgetting new in your array initialization.

int[] btw = new int[]{i,j, k, c4, l, m};

Upvotes: 0

Related Questions