title: Invalid stream header 434B0005 from client to server
body: <p>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.</p>
  
  <p>Here are the objects I'm serializing:</p>
  
  <pre><code>    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();
      }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>    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();
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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 </p>
  
  <pre><code>Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 434B0005
      at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
      at java.io.ObjectInputStream.&lt;init&gt;(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)
  </code></pre>
  
  <p>where Hand.java.29 points at the line</p>
  
  <pre><code>        ObjectInputStream o = new ObjectInputStream(b);
  </code></pre>
  
  <p>in the Hand deserialization method.</p>
  
  <p>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?</p>
  
  <p>I'm only sending a solitary object at a time, so I'm not initializing multiple ObjectInputStreams or anything.</p>