title: ObjectInputStream - StreamCorruptedException: invalid type code: 00
body: <p>First off, I know that there are a number of questions on this exact exception, but none of them seem to have good answers.</p>
  
  <p>I have a local network of clients communicating with a server over TCP sockets.  They are receiving large amounts of data from the server.  When I kick off my test, it runs fairly well for a while, but eventually I see this exception on one of the streams.</p>
  
  <pre><code>java.io.StreamCorruptedException: invalid type code: 00
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1373)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)
      at myProgram.connectionProtocol.waitForMsg(connectionProtocol.java:137)
      at myProgram.Client.ClientProcess$testController.run(ClientProcess.java:296)
      at myProgram.Client.ClientProcess.main(ClientProcess.java:71)
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p><strong>I've tried forcing each process to wait for an Acknowledge message before continuing, and sending a Resend if this error is encountered.  That adds a lot more complexity and seems to cause more problems than it fixes.</strong></p>
  
  <p><strong>I added Buffered Streams (updated in the code) to all of the inputs, having heard that that will improve IO performance, but no luck.  It didn't cause problems though, so I'll leave it in.</strong></p>
  
  <p>Here's some of my code (with junk removed):</p>
  
  <p>server:</p>
  
  <pre><code>private Socket client;
  private String clientName = "?";
  private Boolean threadActive = true;
  private PublicKey clientKey;
  private int timeout = 100;
  
  ClientWorker(Socket sock) {
      client = sock;
  }
  public void run(){
      Object line;
      ObjectInputStream in = null;
      ObjectOutputStream out = null;
  
      client.setSoTimeout(timeout);
      in = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
      out = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
      out.flush();
  
      while(threadActive){
  
          Message msgOut = msgQueue.get(0);
          if(msgOut != null &amp;&amp; msgOut.source.equals(clientName)){
              msgQueue.remove(0);
              msgOut.source = "broker";
              out.writeObject(secure.signObject(msgOut,privKey));
              out.flush();
              out.reset();
          } 
  
          try{
              line = in.readObject();
              handleMessage((SignedObject) line);
          } catch(SocketTimeoutException e){
              //no data currently in stream
          }
      }
      client.close();
  }
  </code></pre>
  
  <p>client:</p>
  
  <pre><code>serverSock = new connectionProtocol(name,privKey);
  socketNum = serverSock.getSocketNum("server");
  serverSock.openSocket(socketNum);
  serverSock.setSoTimeout(100);
  
  while(isActive){
      // ERROR OCCURS IN NEXT LINE
      serverMsg = (SignedObject)serverSock.waitForMsg();
      if(serverMsg != null)
          handleMessage(brokerMsg);
  
      // messages are sent/received over other sockets here
  }
  serverSock.closeSocket();
  </code></pre>
  
  <p>Periodically, another thread will also call serverSock.sendMsg(Message).</p>
  
  <p>connectionProtocol:</p>
  
  <pre><code>public void openSocket(int sockNum) throws IOException {
      socket = new Socket("localhost", sockNum);
      out = new ObjectOutputStream(socket.getOutputStream());
      in = new ObjectInputStream(socket.getInputStream());
      out.flush();
  
  
      Message initMessage = new Message(name,"open");
      SignedObject sigMessage = security.signObject(initMessage, privKey);
      out.writeObject(sigMessage);
      out.flush();
      out.reset();
  
      socketActive = true;
  }
  
  public synchronized void closeSocket(){
      socketActive = false;
      Message closeMessage = new Message(name,"end");
      out.writeObject(security.signObject(closeMessage,privKey));
      out.flush();
      socket.close();
  }
  
  public synchronized Object waitForMsg() throws IOException {
      Object msg = null;
      try {
          // ERROR OCCURS ON NEXT LINE
          msg = in.readObject();
          return msg;
      } catch (SocketTimeoutException e) {
          return null;
      }catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
      return msg;
  }
  
  public synchronized void sendMsg(Message msg){
      out.writeObject(security.signObject(msg, privKey));
      out.flush();
      out.reset();
  }
  </code></pre>