Invalid stream header 434B0005 from client to server-Collection of common programming errors

I’ve serialized some objects so I can convert them to byte arrays for a TCP packet. When I send the objects from the server program to the client program, there are no issues and it works fine. However, even though the code between the server and client is identical, when I try to send objects from the client to the server I get an invalid header.

Here are the objects I’m serializing:

    public static byte[] serialize(Hand c) throws IOException
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(c);
        return baos.toByteArray();
    }

    public static Hand deserialize(byte[] bytes) throws IOException, ClassNotFoundException
    {
        ByteArrayInputStream b = new ByteArrayInputStream(bytes);
        ObjectInputStream o = new ObjectInputStream(b);
        return (Hand) o.readObject();
    }

and

    public static byte[] serialize(Card c) throws IOException
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(c);
        return baos.toByteArray();
    }

    public static Card deserialize(byte[] bytes) throws IOException, ClassNotFoundException
    {
        ByteArrayInputStream b = new ByteArrayInputStream(bytes);
        ObjectInputStream o = new ObjectInputStream(b);
        return (Card) o.readObject();
    }

Those are both taken from the Server program, but the code for the serialization is identical between the server and the client; I important the Card class and the Hand class from the Server to the Client precisely to make sure errors like this wouldn’t occur.

The server can convert a Card or a Hand to a byte[] and write it over a DataOutputStream to the client, and the client can receive the Card or Hand through a DataInputStream, deserialize it, and read it with no problem. When I try to send a Card or a Hand from the Client to the Server, however, very rarely it works and usually I get a

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 434B0005
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.(Unknown Source)
    at Hand.deserialize(Hand.java:29)
    at KoiKoi_TCP_Server.takeClientTurn(KoiKoi_TCP_Server.java:321)
    at KoiKoi_TCP_Server.main(KoiKoi_TCP_Server.java:380)

where Hand.java.29 points at the line

        ObjectInputStream o = new ObjectInputStream(b);

in the Hand deserialization method.

I understand that it’s telling me that the header is invalid. I’m not sure how to fix it, because it only breaks going one direction and the code is identical. Suggestions?

I’m only sending a solitary object at a time, so I’m not initializing multiple ObjectInputStreams or anything.

  1. I’m not initializing multiple ObjectOutputStreams or anything.

    Yes you are. You are initializing a new ObjectOutputStream for every object, and then you’re giving yourself the additional problem of knowing how many bytes to read in order to receive each object, and you’re getting that wrong, so you’re getting out of sync.

    Get rid of all this. You don’t need it. It is just adding problems. Just use a single ObjectOutputStream and ObjectInputStream, directly, for the life of the socket, constructed directly over the socket streams, and call writeObject() when you want to send an object, and readObject() when you want to read one. Two lines of code. Forget about the byte arrays and the ByteArray/DataInput/OutputStreams altogether.