1 Reply Latest reply on Nov 29, 2012 6:22 PM by 976615

    StreamCorruptedException : invalid Type Code AC .. In case of a network app

      I am developing a peer-peer application here.
      What i have is a client and a server on each user, both running parallell.
      User A sends an object to User B through an ObjectOutputStream through the main thread.
      User B reads it using an ObjectInputStream and passes the object that it read, to a new ObjectOutputStream, to be passed to User C.
      User C just reads the incoming object using ObjectInputStream.
      This iteration runs successfully for the first run!
      The moment I go for the second iteration using User A, the method on User B throws a "invalid type code AC".

      class Main{
      Socket sock;

      psvm(String args[]){
      Client newCli = new Client();
      newCli.setSock(<Sets the sock after getting the port and IP address from the user>);
      sock = newCli.getSock();
      ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
      oos.writeObject(new Client());
      Class Server extends thread{
      public void run(){
      ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());

      I read through this post on this forum and "EJP" asks that one OOS and one OIS be maintained per socket.

      So what i did was add a setter method to the Client class which stores the Socket, and called the setter method instead of creating a new ObjectOutputStream object everytime. This way one ObjectOutputStream object would be maintained per socket per client class's instance.

      class Client{
      Socket sock;
      ObjectOutputStream oos;

      //Getter and setter for sock.

      //Getter and setter for ObjectOutputStream
      oos = new ObjectOutputStream(getSock().getOutputStream());

      When i do this, the initial iteration itself fails.
      I am posting the outline of my code since it is a big file. But if writing out a smaller version of the app and posting it here will help better understand the scenario, then I'll do it.
      I am re-posting this, coz in the previous posts regarding this same issue, i found that people were writing and reading on the application. While mine is a case of a client-server send and receive.
      I am just writing multiple times at the client and reading multiple times at the server. And each time i'm creating a fresh ObjectOutputStream object, and Client object.
      I don't close the streams since that closes the sockets, but i need to keep the sockets open and send multiple objects on the same socket.
      The explanation is a bit lengthy, so let me know if something is not clear. :)