title: StreamCorruptedException: invalid type code: 00
body: <p>Server code:</p>
  
  <pre><code>while (true) {                                  
      Socket sock = serv.accept(); 
      try {
          new ClientSession(sock, outQueue, activeSessions);
          System.out.println("CS");
      } catch (IOException e) {
          System.out.println("Sock error");
          sock.close();
      }
  }
  </code></pre>
  
  <p>ClientSession:</p>
  
  <pre><code>class ClientSession extends Thread {
  private Socket socket;
  private OutboundMessages outQueue;
  private ActiveSessions activeSessions;
  private ObjectInputStream netIn;
  private ObjectOutputStream netOut;
  int n = 0;
  boolean inGame = false;
  boolean ready = false;
  Player p;
  
  public ClientSession(Socket s, OutboundMessages out, ActiveSessions as)
          throws IOException {
      socket = s;
      outQueue = out;
      activeSessions = as;
      netOut = new ObjectOutputStream(socket.getOutputStream());
      netOut.flush();
      netIn = new ObjectInputStream(socket.getInputStream());
      System.out.println("ClientSession " + this + " starts...");
      while (true) {
          Object nameMsg = null;
          try {
              nameMsg = netIn.readObject();
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          }
          if (nameMsg instanceof NameMessage) {
              this.setName(((NameMessage) nameMsg).name);
              break;
          }
      }
      start();
  }
  
  public void run() {
      try {
          activeSessions.addSession(this);
          while (true) {
              Object inMsg = null;
              try {
                  try {
                      inMsg = netIn.readObject();
                  } catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
              if (inMsg instanceof ReadyMessage) {
                  ready = true;
              } else if (inMsg instanceof DirMessage) {
                  p.setDir(((DirMessage)inMsg).dir);
              }
  
          }
      } finally {
          try {
              socket.close();
          } catch (IOException e) {
          }
      }
  }
  
  public void sendMessage(Message msg) {
      try {
          if (!socket.isClosed()) {
              netOut.writeObject(msg);
              netOut.flush();
          } else {
              throw new IOException();
          }
      } catch (IOException eee) {
          try {
              socket.close();
          } catch (IOException ee) {
          }
      }
  }
  </code></pre>
  
  <p>Creating input and output on client side:</p>
  
  <pre><code>public void connect() {
      try {
          InetAddress serverAddr = InetAddress.getByName(serverName);
          try {
              System.out.println("Connecting with "
                      + serverAddr.getHostName() + ":" + port);
              socket = new Socket(serverAddr, port);
  
              try {
                  System.out.println("Connected to "
                          + serverAddr.getHostName());
                  netOut = new ObjectOutputStream(socket.getOutputStream());
                  netOut.flush();
                  netIn = new ObjectInputStream(socket.getInputStream());
                  netOut.writeObject(new NameMessage(name));
                  netOut.flush();
              } finally {
  
              }
  
          } catch (ConnectException e) {
              System.out.println("Cannot connect to server");
  
          } catch (IOException e) {
              System.out.println("Input error");
          }
  
      } catch (UnknownHostException e) {
          System.out.println("Unknown server: " + e.getMessage());
      }
  }
  </code></pre>
  
  <p>receiver on client end:</p>
  
  <pre><code>public void run() {
      while (true) {
          Object a = null;
  
              try {
                  a = netIn.readObject();
                  netIn.reset();
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
          if (a != null &amp;&amp; a instanceof CoordMessage) {
              setXY((CoordMessage)a);
          }
      }
  }
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <pre><code>java.io.StreamCorruptedException: invalid type code: 00
  at java.io.ObjectInputStream.readObject0(Unknown Source)
  at java.io.ObjectInputStream.readObject(Unknown Source)
  at twoPlayerClient.Receiver.run(Receiver.java:28)
  </code></pre>
  
  <p>After creating input and output I keep passing them on to other classes and not creating new ones.</p>
  
  <p>I have read other similar questions but can't find an answer to why this keeps happening.</p>